Ver código fonte

make malloc(0) return a unique pointer value

malloc(0) was returning a null pointer and this caused problems to several
applications. It returns now a unique pointer value which can later be
successfully passed to free().
Ioannis Koutras 12 anos atrás
pai
commit
e7505ae95a
3 arquivos alterados com 5 adições e 3 exclusões
  1. 3 1
      include/dmmlib/dmmlib.h
  2. 1 1
      src/free.c
  3. 1 1
      src/malloc.c

+ 3 - 1
include/dmmlib/dmmlib.h

@@ -38,7 +38,9 @@ allocator_t systemallocator;
 #if !defined _STDLIB_H && !defined _STDLIB_H_
 
 /**
- * Allocates size bytes by using the system's allocator.
+ * Allocates size bytes by using the system's allocator. The memory is not
+ * initialized. If size is 0, then malloc() returns a unique pointer value that
+ * can later be successfully passed to free().
  * @param size Requested allocation size in bytes.
  *
  * @return The pointer to the memory location.

+ 1 - 1
src/free.c

@@ -37,7 +37,7 @@
 void free(void *ptr) {
     raw_block_header_t *owner_raw_block;
 
-    if(ptr == NULL) {
+    if(ptr == NULL || ptr == 0xD34D) {
         return;
     }
 

+ 1 - 1
src/malloc.c

@@ -45,7 +45,7 @@ void * malloc(size_t size) {
     ptr = NULL;
 
     if(size == 0) {
-        return NULL;
+        return 0xD34D;
     }
     
     /* Try to find a raw block available for allocation */