[fpc-devel] Const optimization is a serious bug

Alexander Klenin klenin at gmail.com
Thu Jul 7 17:27:17 CEST 2011


On Fri, Jul 8, 2011 at 01:48, Martin <fpc at mfriebe.de> wrote:
>> If you look at the point (2) of the mail you responded to,
>> you will see that I discuss this exact issue. "Const string" is passed by
>> value,
>> and it does not lead to any undefined behaviour and crashes
>> except this case (barring, of course, as-yet-unknown compiler bugs).

> (code below written in my mailer, so my have typos, but should be enough to
> get the point
[example skipped]

Above, I have quoted the relevant part of the mail you responded to.
If you read it, you will note that it discusses exactly the case
you tried to demonstrate.
Your example (after fixes identified below) proves my points, which
are, yet again:

1) That the code you posted would work quite correctly if "const
string" is fixed,
  including all the optimizations you are suggesting in the rest of
your message.
2) That it works correctly in (recent versions of) Delphi
3) That is will break in unpredictable fashion due to the bug we are
talking about.

Note: besides minor typos, you example does not cause error due to unrelated
issue: for some reason, string value returned from function has a refcount of 2
instead of 1.
I replaced call to "Copy" by "'x' + Bar + 'x'" to ensure refcount of 1.

-- 
Alexander S. Klenin



More information about the fpc-devel mailing list