Browse Source

LibC: Don't leak memory for realloc(p, 0)

Previously we'd leak memory when the user called realloc(p, 0). Instead
this call should behave as if the user had called free(p).
Gunnar Beutner 4 years ago
parent
commit
ea33e9647b
1 changed files with 3 additions and 1 deletions
  1. 3 1
      Userland/Libraries/LibC/malloc.cpp

+ 3 - 1
Userland/Libraries/LibC/malloc.cpp

@@ -437,8 +437,10 @@ void* realloc(void* ptr, size_t size)
 {
 {
     if (!ptr)
     if (!ptr)
         return malloc(size);
         return malloc(size);
-    if (!size)
+    if (!size) {
+        free(ptr);
         return nullptr;
         return nullptr;
+    }
 
 
     Threading::Locker locker(malloc_lock());
     Threading::Locker locker(malloc_lock());
     auto existing_allocation_size = malloc_size(ptr);
     auto existing_allocation_size = malloc_size(ptr);