* fixed pointer arithmetic errors in WideStrAlloc/StrBufSize/StrDispose

(patch by Iks, mantis #29710)

git-svn-id: trunk@33271 -
This commit is contained in:
Jonas Maebe 2016-03-18 13:12:11 +00:00
parent 697aed738c
commit 2b210335a1
3 changed files with 20 additions and 5 deletions

1
.gitattributes vendored
View File

@ -13220,6 +13220,7 @@ tests/test/units/sysutils/tstrcmp.pp svneol=native#text/plain
tests/test/units/sysutils/tstrtobool.pp svneol=native#text/plain
tests/test/units/sysutils/tunifile.pp svneol=native#text/plain
tests/test/units/sysutils/tuplow.pp svneol=native#text/plain
tests/test/units/sysutils/twstralloc.pp svneol=native#text/plain
tests/test/units/sysutils/twstrcmp.pp svneol=native#text/plain
tests/test/units/ucomplex/tcsqr1.pp svneol=native#text/pascal
tests/test/units/variants/tcustomvariant.pp svneol=native#text/plain

View File

@ -468,14 +468,14 @@ function strnew(p : pwidechar) : pwidechar; overload;
function WideStrAlloc(Size: cardinal): PWideChar;
begin
getmem(result,size*2+sizeof(cardinal));
cardinal(pointer(result)^):=size*2+sizeof(cardinal);
inc(result,sizeof(cardinal));
PCardinal(result)^:=size*2+sizeof(cardinal);
result:=PWideChar(PByte(result)+sizeof(cardinal));
end;
function StrBufSize(str: pwidechar): cardinal;
begin
if assigned(str) then
result:=cardinal(pointer(str-sizeof(cardinal))^)-sizeof(cardinal)
result:=(PCardinal(PByte(str)-sizeof(cardinal))^)-sizeof(cardinal)
else
result := 0;
end;
@ -484,8 +484,8 @@ procedure StrDispose(str: pwidechar);
begin
if assigned(str) then
begin
dec(str,sizeof(cardinal));
freemem(str,cardinal(pointer(str)^));
str:=PWideChar(PByte(str)-sizeof(cardinal));
freemem(str,PCardinal(str)^);
end;
end;

View File

@ -0,0 +1,14 @@
{ %opt=-g-h }
uses
sysutils;
var
pw: pwidechar;
begin
pw:=widestralloc(1);
pw^:='a';
if StrBufSize(pw)<>2 then
halt(1);
StrDispose(pw);
end.