瀏覽代碼

Prevent overflow of new_size, check if new_size is also equal to min_split_size, mark block as used regardless of splitting's activation.

Ioannis Koutras 13 年之前
父節點
當前提交
6312088e35
共有 1 個文件被更改,包括 13 次插入4 次删除
  1. 13 4
      src/custom_malloc.c

+ 13 - 4
src/custom_malloc.c

@@ -86,7 +86,16 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 
         /* Try to split */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-        new_size = get_size(ptr) - size - HEADER_SIZE;
+        /* Check what would be the size of the new block if we split the current
+         * one.
+         * Note: new_size is a size_t, so compare first in order to prevent an
+         * underflow.
+         */
+        if(get_size(ptr) > size + HEADER_SIZE) {
+            new_size = get_size(ptr) - size - HEADER_SIZE;
+        } else {
+            new_size = 0;
+        }
 
 #ifdef SPLITTING_FIXED
         min_split_size = MIN_SPLITTING_SIZE;
@@ -95,13 +104,13 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
         min_split_size = heap->dmm_knobs.min_split_size;
 #endif /* SPLITTING_VARIABLE */
 
-        if(new_size > min_split_size) {
+        if(new_size >= min_split_size) {
             split(allocator, heap, ptr, size);
         }
-#else
-        mark_used(ptr);
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
+        mark_used(ptr);
+
 #ifdef FUTURE_FEATURES
         /* Update the used blocks list */
         push_block(&ptr, &heap->used_blocks_head);