[fpc-pascal] Stack corruption setting UnicodeString Char

Sven Barth pascaldragon at googlemail.com
Wed Oct 19 09:48:41 CEST 2011


Am 19.10.2011 09:13, schrieb Felipe Monteiro de Carvalho:
> Hello,
>
> Usually when we work with PChars we do things like this to set the
> final terminator:
>
> var
>    ansistr: ansistring;
> begin
>    // Copy the originating string taking into account the specified length
>    SetLength(ansistr, len+1);
>    System.Move(source^, ansistr[1], len);
>    ansistr[len+1] := #0;
>    SetLength(ansistr, len);
>
> And it works fine. You get a string with correct length and a final #0
> terminator.
>
> Now, I simply tryed the same code for a unicodestring, and it corrupts my stack:
>
> procedure Unicode2AnsiMove(source:pwidechar;var dest:ansistring;len:SizeInt);
> var
>    widestr: unicodestring;
> begin
>    {$ifdef PASWSTRING_VERBOSE}WriteLn('Unicode2AnsiMove START');{$endif}
>    // Copy the originating string taking into account the specified length
>    SetLength(widestr, len+1);
>    System.Move(source^, widestr[1], len*2);
>    PWideChar(@widestr)[len] := #0; //<--- This corrupts the stack
>    SetLength(widestr, len);

Why don't you use the same statement as for the AnsiString version?

E.g.

widestr[len+1] := #0;

Regards,
Sven



More information about the fpc-pascal mailing list