[fpc-devel] One more issue with string function results (hopefully, the last)

Sergei Gorelkin sergei_gorelkin at mail.ru
Tue Jan 22 18:14:48 CET 2008


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.



More information about the fpc-devel mailing list