From e72c1f9cb11c0dd19ed4ba668c24c71d7eed0e4d Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sun, 12 Aug 2007 10:51:56 +0000 Subject: [PATCH] Avoid a crash in realloc() if the allocation of the new block failed. Return NULL instead. Someone please review. Should errno be set to ENOMEM here? git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21903 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/system/libroot/posix/malloc/wrapper.cpp | 5 +++++ 1 file changed, 5 insertions(+) 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.