Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2008-01

One more issue with string function results (hopefully, the last)

2 messages · 1 person · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Sergei Gorelkin

Hello,

Ran into it while compiling Lazarus. It appears that, while many sophisticated checks are done in tcallnode.maybe_create_funcret_node, the obvious case when function result destination is one of the function's arguments, is not checked. To be honest, I noticed that earlier for the case of s := Copy(s, ...), but was thiniking that there is some compiler magic around Copy(). Delphi also appears to be smart enough to omit temps in case of s := copy(s,...).
Anyway, since copy() assigns its result once before exiting, it works fine. Things go wrong when the function is tries to access its argument after modifying the result, like in the attached example.

Sergei

-------------------------------------
program test;
{$ifdef fpc}{$mode objfpc}{$H+}{$endif}
const buf: array[0..5] of char = 'abcdef';
function foo(const a: string): string;
begin
   SetLength(result, 6);
   Move(buf, result[1], sizeof(buf));
   if a  '1234567890' then
     writeln('Failed')
   else
     writeln('ok');
end;
procedure test_proc;
var
   s: string;
begin
   s := '1234567890';
   s := foo(s);             // This one failes
   writeln(s);
end;
var
   s: string;
begin
   test_proc;
   s := '1234567890';
   s := foo(s);            // But this one is correct (called from main
program level?)
   writeln(s);
end.

Reply by email

Sergei Gorelkin
to Sergei Gorelkin
Sergei Gorelkin wrote

No, it wasn't the last one, found yet another :(
Again, nothing in testsuite could detect it, so I wrote one.

A thing to note is that two last tests (pchar_to_widestr and chararray_to_widestr) currently don't fail at least on win32 because compiler keeps generating a temp widestring for function result. This may indicate issues with typeconv nodes handling.

The attached RTL patch is 'safe' in the sense it is independent of compiler changes and wouldn't break the old code - just slow it down a tiny bit.

Reply by email