Skip to content

RTTI.Invoke string treatment depends on {$H} setting

Summary

With {$H+}, string passed to RTTI.Invoke is accessed correctly. Otherwise, it's like trying to treat a shortstring as an ansistring.

System Information

  • Operating system: Linux
  • Processor architecture: x86_64
  • Compiler version: bb182470
  • Device: Laptop

Steps to reproduce

Compile and run the following code:

{$ H+}
uses
  rtti,
  typinfo,
{$ifndef win64}
  ffi.manager;
{$endif}

procedure p(s: String);
begin
  WriteLn(s);
end;

var
  a: TValueArray;
begin
  a := ['hello'];
  Invoke(@p,a,ccReg,nil,false,false);
end.

The output will be garbage like: ello�?@�?� @�?���

Change the {$ H+} into {$H+} and it will output: hello

Example Project

What is the current bug behavior?

Seems like a shortstring is treated as an ansistring

What is the expected (correct) behavior?

With or without {$H+}, it should output: hello

Relevant logs and/or screenshots

Possible fixes

I think this is due to missing operator := definition for shortstring in TValue record, but somehow the compiler managed to think that the shortstring can be treated like an ansistring.

Declaring:

operator := (const AValue: ShortString) Result: TValue; inline;
begin
  TValue.Make(@AValue, System.TypeInfo(AValue), Result);
end;

in above program is sufficient to solve the problem. A proper implementation has been done in the linked merge request, tested and proven to be working.

Edited by Mario Ray Mahardhika
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information