[fpc-devel] Const optimization is a serious bug
    michael.vancanneyt at wisa.be 
    michael.vancanneyt at wisa.be
       
    Thu Jul  7 17:29:08 CEST 2011
    
    
  
On Fri, 8 Jul 2011, Alexander Klenin wrote:
> On Fri, Jul 8, 2011 at 00:14,  <michael.vancanneyt at wisa.be> wrote:
>> Given that Borland never decided to 'fix' it, I'm inclined to think that
>> they also don't consider it a real problem, but rather a corner case
>> (if they are aware of it at all).
>
> Hm. My testing indicates that Delphi has this fixed since at least D2007.
No, it did not.
program tests;
{$APPTYPE CONSOLE}
uses
   SysUtils;
Var
   A : ansistring;
Procedure DoIt(Const B : ansistring);
begin
   A:='Something else';
   Writeln(B);
end;
begin
   A:='Something';
   DoIt(A);
   Readln;
end.
Writes 'Something', because the pointer B points to a block that has not
yet been invalidated. It works by accident.
Changing it to:
program tests;
{$APPTYPE CONSOLE}
uses
   SysUtils;
Var
   A : ansistring;
Procedure DoIt(Const B : ansistring);
begin
   A:='Something else';
   Writeln(B+' aha !!');
end;
begin
   A:='Something';
   DoIt(A);
   Readln; 
end.
The program crashes.
If you look at the generated assembler code, you'll see that no reference
count increasing is done in DoIt.
Michael.
    
    
More information about the fpc-devel
mailing list