瀏覽代碼

* calculate middle element P correctly if (L+R>=2^31), resolves #28289

git-svn-id: trunk@31267 -
florian 10 年之前
父節點
當前提交
a797c2fc46
共有 1 個文件被更改,包括 6 次插入4 次删除
  1. 6 4
      rtl/objpas/fgl.pp

+ 6 - 4
rtl/objpas/fgl.pp

@@ -667,18 +667,18 @@ var
   i : integer;
   i : integer;
   pdest,
   pdest,
   psrc : Pointer;
   psrc : Pointer;
-  localnul : array[0..LocalThreshold-1] of byte;  
+  localnul : array[0..LocalThreshold-1] of byte;
   pnul : pointer;
   pnul : pointer;
 begin
 begin
   LItemSize:=FItemSize;
   LItemSize:=FItemSize;
   pnul:=@localnul;
   pnul:=@localnul;
   if LItemSize>Localthreshold then
   if LItemSize>Localthreshold then
     getmem(pnul,LItemSize);
     getmem(pnul,LItemSize);
-  fillchar(pnul^,LItemSize,#0);    
+  fillchar(pnul^,LItemSize,#0);
   NewCount:=0;
   NewCount:=0;
   psrc:=First;
   psrc:=First;
   pdest:=psrc;
   pdest:=psrc;
-  
+
   For I:=0 To FCount-1 Do
   For I:=0 To FCount-1 Do
     begin
     begin
         if not CompareMem(psrc,pnul,LItemSize) then
         if not CompareMem(psrc,pnul,LItemSize) then
@@ -707,7 +707,9 @@ begin
   repeat
   repeat
     I := L;
     I := L;
     J := R;
     J := R;
-    P := (L + R) div 2;
+    { cast to dword to avoid overflow to a negative number during addition which
+      would result again in a negative number when being divided }
+    P := (dword(L) + dword(R)) div 2;
     repeat
     repeat
       PivotItem := InternalItems[P];
       PivotItem := InternalItems[P];
       while Compare(PivotItem, InternalItems[I]) > 0 do
       while Compare(PivotItem, InternalItems[I]) > 0 do