Browse Source

Account for MALLOC_HEADER_SIZE when calculating new size.

Miles Bader 23 năm trước cách đây
mục cha
commit
73900d95a7
1 tập tin đã thay đổi với 4 bổ sung3 xóa
  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);