2
0
Эх сурвалжийг харах

* fixed pointer arithmetic errors in WideStrAlloc/StrBufSize/StrDispose
(patch by Iks, mantis #29710)

git-svn-id: trunk@33271 -

Jonas Maebe 9 жил өмнө
parent
commit
2b210335a1

+ 1 - 0
.gitattributes

@@ -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/tstrtobool.pp svneol=native#text/plain
 tests/test/units/sysutils/tunifile.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/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/sysutils/twstrcmp.pp svneol=native#text/plain
 tests/test/units/ucomplex/tcsqr1.pp svneol=native#text/pascal
 tests/test/units/ucomplex/tcsqr1.pp svneol=native#text/pascal
 tests/test/units/variants/tcustomvariant.pp svneol=native#text/plain
 tests/test/units/variants/tcustomvariant.pp svneol=native#text/plain

+ 5 - 5
rtl/objpas/sysutils/sysuni.inc

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

+ 14 - 0
tests/test/units/sysutils/twstralloc.pp

@@ -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.