SIGSEGV when modifying a PChar assigned from a string constant
Original Reporter info from Mantis: kluug.net @onpok
-
Reporter name: Ondrej Pokorny
Original Reporter info from Mantis: kluug.net @onpok
- Reporter name: Ondrej Pokorny
Description:
If you assign a constant to a string variable and then try to modify the variable via a PChar, a SIGSEGV is raised.
Steps to reproduce:
program PCharTest;
var
Str: AnsiString;
P: PAnsiChar;
begin
Str := 'hello';
P := PAnsiChar(Str);
P[1] := 'x'; // SIGSEGV in FPC, OK in Delphi
end.
Additional information:
If Str points a read-only buffer, FPC has to recreate it and assign a read/write buffer at latest when Str is assigned to a PAnsiChar/Pointer - in this line of my example:
P := PAnsiChar(Str);
- or -
do it like Delphi does it - Delphi generates a read/write copy in Str during the assignment so that there is never a read-only buffer in a string variable:
program ConstStringTest;
var
Str: AnsiString;
begin
Str := 'hello'; // Delphi: new string is generated here in Str
Writeln(PInteger(PByte(Str) - 8)^); // 1 in Delphi, -1 in FPC
ReadLn;
end.
Mantis conversion info:
- Mantis ID: 35461
- Version: 3.3.1
- Monitored by: » Xor-el (Ugochukwu Mmaduekwe)