Przeglądaj źródła

Fix free() to accept NULL pointers

Rafał Wojdyła 5 lat temu
rodzic
commit
b46496a379
3 zmienionych plików z 7 dodań i 2 usunięć
  1. 2 0
      LibOS/shim/src/shim_malloc.c
  2. 1 2
      Pal/lib/graphene/config.c
  3. 4 0
      Pal/src/slab.c

+ 2 - 0
LibOS/shim/src/shim_malloc.c

@@ -285,6 +285,8 @@ void __free_debug (void * mem, const char * file, int line)
 void free (void * mem)
 #endif
 {
+    if (!mem)
+        return;
     if (MEMORY_MIGRATED(mem)) {
         INC_PROFILE_OCCURENCE(free_migrated);
         return;

+ 1 - 2
Pal/lib/graphene/config.c

@@ -373,8 +373,7 @@ int free_config (struct config_store * store)
 {
     struct config * e, * n;
     listp_for_each_entry_safe(e, n, &store->entries, list) {
-        if (e->buf)
-            store->free(e->buf);
+        store->free(e->buf);
         store->free(e);
     }
 

+ 4 - 0
Pal/src/slab.c

@@ -71,6 +71,8 @@ static inline void * __malloc (int size)
 
 static inline void __free (void * addr, int size)
 {
+    if (!addr)
+        return;
 #if STATIC_SLAB == 1
     if (addr >= (void *)mem_pool && addr < mem_pool_end)
         return;
@@ -168,6 +170,8 @@ void * calloc (size_t nmem, size_t size)
 
 void free (void * ptr)
 {
+    if (!ptr)
+        return;
 #if PROFILING == 1
     unsigned long before_slab = _DkSystemTimeQuery();
 #endif