* fixed null-termination in StringToWideChar() if the buffer is larger than

the string (mantis #22669)

git-svn-id: trunk@22124 -
This commit is contained in:
Jonas Maebe 2012-08-19 13:48:02 +00:00
parent b353a2ff27
commit 504544e173
4 changed files with 40 additions and 15 deletions

1
.gitattributes vendored
View File

@ -12783,6 +12783,7 @@ tests/webtbs/tw22593.pp svneol=native#text/plain
tests/webtbs/tw2260.pp svneol=native#text/plain
tests/webtbs/tw22613.pp svneol=native#text/plain
tests/webtbs/tw2266.pp svneol=native#text/plain
tests/webtbs/tw22669.pp svneol=native#text/plain
tests/webtbs/tw2267.pp svneol=native#text/plain
tests/webtbs/tw2268.pp svneol=native#text/plain
tests/webtbs/tw2269.pp svneol=native#text/plain

View File

@ -1030,16 +1030,15 @@ function WideCharToString(S : PWideChar) : UnicodeString;
{$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
var
temp:widestring;
temp: widestring;
Len: SizeInt;
begin
widestringmanager.Ansi2WideMoveProc(PChar(Src),StringCodePage(Src),temp,Length(Src));
if Length(temp)<DestSize then
move(temp[1],Dest^,Length(temp)*SizeOf(WideChar))
else
move(temp[1],Dest^,(DestSize-1)*SizeOf(WideChar));
Dest[DestSize-1]:=#0;
Len:=Length(temp);
if DestSize<=Len then
Len:=Destsize-1;
move(temp[1],Dest^,Len*SizeOf(WideChar));
Dest[Len]:=#0;
result:=Dest;
end;
{$endif FPC_HAS_STRING_LEN_TO_WIDECHAR}

View File

@ -592,15 +592,16 @@ end;
{$define FPC_HAS_STRING_LEN_TO_WIDECHAR}
function StringToWideChar(const Src : RawByteString;Dest : PWideChar;DestSize : SizeInt) : PWideChar;
var
temp:widestring;
temp: widestring;
Len: SizeInt;
begin
temp:=src;
if Length(temp)<DestSize then
JLString(temp).getChars(0,length(temp),TJCharArray(Dest),0)
else
JLString(temp).getChars(0,DestSize-1,TJCharArray(Dest),0);
Dest[DestSize-1]:=#0;
result:=Dest;
Len:=Length(temp);
if DestSize<=Len then
Len:=Destsize-1;
JLString(temp).getChars(0,Len,TJCharArray(Dest),0);
Dest[Len]:=#0;
result:=Dest;
end;

24
tests/webtbs/tw22669.pp Normal file
View File

@ -0,0 +1,24 @@
program tw22669;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$else}
{$APPTYPE CONSOLE}
{$endif}
var buf:array[1..11] of widechar;
s:ansistring;
begin
buf:='isnotempty';
s:='test';
StringToWideChar(s,@buf[1],10);
s:=widestring(pwidechar(@buf[1]));
if s<>'test' then
halt(1);
s:='0123456789';
StringToWideChar(s,@buf[1],10);
s:=widestring(pwidechar(@buf[1]));
if s<>'012345678' then
halt(2);
end.