1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 15:08:12 +00:00

LibC: Fix busted realloc() implementation.

This commit is contained in:
Andreas Kling 2019-02-15 22:36:59 +01:00
parent df6aaaeeef
commit 3b42db0b4c

View file

@ -9,6 +9,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <Kernel/Syscall.h> #include <Kernel/Syscall.h>
#include <AK/StdLibExtras.h>
extern "C" { extern "C" {
@ -160,8 +161,11 @@ void* realloc(void *ptr, size_t size)
validate_mallocation(ptr, "realloc()"); validate_mallocation(ptr, "realloc()");
auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader))); auto* header = (MallocHeader*)((((byte*)ptr) - sizeof(MallocHeader)));
size_t old_size = header->size; size_t old_size = header->size;
if (size == old_size)
return ptr;
auto* new_ptr = malloc(size); auto* new_ptr = malloc(size);
memcpy(new_ptr, ptr, old_size); memcpy(new_ptr, ptr, min(old_size, size));
free(ptr);
return new_ptr; return new_ptr;
} }