浏览代码

* tinyheap: Early check for very big requested mem sizes in order to prevent overflows and properly report the out of memory error.

git-svn-id: trunk@45844 -
yury 5 年之前
父节点
当前提交
6edbc9ed7e
共有 1 个文件被更改,包括 5 次插入0 次删除
  1. 5 0
      rtl/inc/tinyheap.inc

+ 5 - 0
rtl/inc/tinyheap.inc

@@ -57,6 +57,7 @@
 
 
     const
     const
       TinyHeapMinBlock = sizeof(TTinyHeapBlock);
       TinyHeapMinBlock = sizeof(TTinyHeapBlock);
+      TinyHeapMaxBlock = High(ptruint) - sizeof(TTinyHeapBlock) - sizeof(TTinyHeapMemBlockSize);
 
 
       TinyHeapAllocGranularity = sizeof(TTinyHeapBlock);
       TinyHeapAllocGranularity = sizeof(TTinyHeapBlock);
 
 
@@ -96,6 +97,8 @@
 {$ifdef DEBUG_TINY_HEAP}
 {$ifdef DEBUG_TINY_HEAP}
         Write('SysGetMem(', Size, ')=');
         Write('SysGetMem(', Size, ')=');
 {$endif DEBUG_TINY_HEAP}
 {$endif DEBUG_TINY_HEAP}
+        if size>TinyHeapMaxBlock then
+          HandleError(203);
         AllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
         AllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
 
 
         p := FreeList;
         p := FreeList;
@@ -320,6 +323,8 @@
             if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
             if (TTinyHeapPointerArithmeticType(p) < TTinyHeapPointerArithmeticType(HeapOrg)) or
                (TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
                (TTinyHeapPointerArithmeticType(p) >= TTinyHeapPointerArithmeticType(HeapPtr)) then
               HandleError(204);
               HandleError(204);
+            if size>TinyHeapMaxBlock then
+              HandleError(203);
             oldsize := FindSize(p);
             oldsize := FindSize(p);
             OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
             OldAllocSize := align(oldsize+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
             NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);
             NewAllocSize := align(size+sizeof(TTinyHeapMemBlockSize), TinyHeapAllocGranularity);