StringToWideChar does not copy the terminating #0 of the string resulting in a lot of garbage at the end of the string.
Original Reporter info from Mantis: ludob
-
Reporter name: Ludo Brands
Original Reporter info from Mantis: ludob
- Reporter name: Ludo Brands
Description:
The program in Steps to Reproduce results in a WideChar string 'testtempt' while the same program in Delphi 2006 results in 'test'. Reason is that StringToWideChar uses only the characters in the input string Src but not the trailing #0.
In real life programs where Dest is not initialised the result is sometimes a lot of garbage characters.
Steps to reproduce:
program Project1;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$else}
{$APPTYPE CONSOLE}
{$endif}
uses
SysUtils;
var buf:widestring;
s:ansistring;
begin
buf:='isnotempty';
s:='test';
StringToWideChar(s,@buf[1],10);
s:=widestring(pwidechar(@buf[1]));
writeln(s);
end.
Additional information:
The testprogram has some casts that are needed for Delphi. The test in its simplest form is:
program Project1;
{$mode objfpc}{$H+}
var buf:array[0..10] of Widechar;
s:string;
begin
buf:='isnotempty';
s:='test';
StringToWideChar(s,buf,10);
s:=buf;
writeln(s);
end.
Mantis conversion info:
- Mantis ID: 22669
- Build: 21971
- Version: 2.7.1
- Fixed in version: 3.0.0
- Fixed in revision: 22124 (#504544e1)