瀏覽代碼

Account for MALLOC_HEADER_SIZE when calculating new size.

Miles Bader 23 年之前
父節點
當前提交
73900d95a7
共有 1 個文件被更改,包括 4 次插入3 次删除
  1. 4 3
      libc/stdlib/malloc/realloc.c

+ 4 - 3
libc/stdlib/malloc/realloc.c

@@ -28,9 +28,10 @@ realloc (void *mem, size_t new_size)
       char *base_mem = MALLOC_BASE (mem);
       size_t size = MALLOC_SIZE (mem);
 
-      /* Make sure that we're dealing in a multiple of the heap allocation
-	 unit (SIZE is already guaranteed to be so).  */
-      new_size = HEAP_ADJUST_SIZE (new_size);
+      /* Include extra space to record the size of the allocated block.
+	 Also make sure that we're dealing in a multiple of the heap
+	 allocation unit (SIZE is already guaranteed to be so).*/
+      new_size = HEAP_ADJUST_SIZE (new_size + MALLOC_HEADER_SIZE);
 
       MALLOC_DEBUG ("realloc: 0x%lx, %d (base = 0x%lx, total_size = %d)\n",
 		    (long)mem, new_size, (long)base_mem, size);