summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-15 22:36:59 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-15 22:36:59 +0100
commit3b42db0b4ca48460754b07449037a1fc660c577a (patch)
treea46354363e2fd912ef939426245fc25e8c5a77aa
parentdf6aaaeeef5777dbf3bbdff6b4651ff891fa5a35 (diff)
downloadserenity-3b42db0b4ca48460754b07449037a1fc660c577a.zip
LibC: Fix busted realloc() implementation.
-rw-r--r--LibC/stdlib.cpp6
1 files changed, 5 insertions, 1 deletions
diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp
index 06e48cf4cf..4adaa0ee81 100644
--- a/LibC/stdlib.cpp
+++ b/LibC/stdlib.cpp
@@ -9,6 +9,7 @@
#include <AK/Assertions.h>
#include <AK/Types.h>
#include <Kernel/Syscall.h>
+#include <AK/StdLibExtras.h>
extern "C" {
@@ -160,8 +161,11 @@ void* realloc(void *ptr, size_t size)
validate_mallocation(ptr, "realloc()");
auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
size_t old_size = header->size;
+ if (size == old_size)
+ return ptr;
auto* new_ptr = malloc(size);
- memcpy(new_ptr, ptr, old_size);
+ memcpy(new_ptr, ptr, min(old_size, size));
+ free(ptr);
return new_ptr;
}