浏览代码

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

git-svn-id: trunk@33271 -

Jonas Maebe 9 年之前
父节点
当前提交
2b210335a1
共有 3 个文件被更改,包括 20 次插入5 次删除
  1. 1 0
      .gitattributes
  2. 5 5
      rtl/objpas/sysutils/sysuni.inc
  3. 14 0
      tests/test/units/sysutils/twstralloc.pp

+ 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/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

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

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

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