diff --git a/src/system/libroot/posix/malloc/wrapper.cpp b/src/system/libroot/posix/malloc/wrapper.cpp index 900355fc73..64cf5fdaa6 100644 --- a/src/system/libroot/posix/malloc/wrapper.cpp +++ b/src/system/libroot/posix/malloc/wrapper.cpp @@ -285,6 +285,11 @@ realloc(void *ptr, size_t size) // Allocate a new block of size sz. void *buffer = malloc(size); + if (buffer == NULL) { + // Allocation failed, free old block and return + free(ptr); + return NULL; + } // Copy the contents of the original object // up to the size of the new block.