UniqueString is not always called, as it should be
Original Reporter info from Mantis: Marcin Wiazowski
-
Reporter name:
Original Reporter info from Mantis: Marcin Wiazowski
- Reporter name:
Description:
The problem described below occurs only in Delphi mode. Please see the attached Reproduce1 and Reproduce2 demos.
When compiled with Delphi, output of both demos is as expected:
aaaaaaaa
aXaaaaaa
When compiled with FPC (NOT in Delphi mode), output of both demos is also as expected:
aaaaaaaa
aXaaaaaa
When compiled with FPC (in Delphi mode), Reproduce1 returns Runtime error 216, while Reproduce2's output is:
aXaaaaaa
aXaaaaaa
Explanation: Reproduce1 contains the following code:
var
Str1, Str2 : string;
P : PChar;
begin
Str1:='aaaaaaaa'; // Str1 points now to the read-only memory (i.e. to executable's code section)
Str2:=Str1; // Str2 points now to the same memory address as Str1
// FPC should internally generate the following call here (as Delphi does):
// UniqueString(Str2);
P:=@Str2[2];
P^:='X'; // Due to lacking "UniqueString(Str2)" call, Str2 still points to
// the read-only memory, so FPC causes access violation here
Similar problem occurs in Reproduce2.
Solution: when access to string's memory is detected (in the "P:=@str2[2]" statement), compiler should internally insert an "UniqueString(Str2)" call before - as it is currently in non-Delphi mode.
After removing the {$MODE DELPHI} directive, the problem no longer occurs. Tested on Win32 platform, with FPC 3.0.4, 3.2.0 (r44257) and 3.3.1 (r44257).
Mantis conversion info:
- Mantis ID: 36755
- Build: 44257
- Version: 3.3.1