diff options
author | Benny Baumann <BenBE@geshi.org> | 2020-10-15 07:38:28 +0200 |
---|---|---|
committer | Benny Baumann <BenBE@geshi.org> | 2020-10-17 20:54:14 +0200 |
commit | c138d148974ab383044d1eec9922127faeb020f0 (patch) | |
tree | cc18c47e64f9082db670bd8211dbb285eb66065c /XUtils.c | |
parent | 5e4b1826168b74d8b5e71227ded12980efd5a243 (diff) |
Release old memory on error
Avoids leaking memory upon realloc failure.
Diffstat (limited to 'XUtils.c')
-rw-r--r-- | XUtils.c | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -13,6 +13,7 @@ in the source distribution for its full text. #include <stdlib.h> #include <string.h> #include <strings.h> +#include <unistd.h> #include "CRT.h" @@ -20,6 +21,8 @@ in the source distribution for its full text. void fail() { CRT_done(); abort(); + + _exit(1); // Should never reach here } void* xMalloc(size_t size) { @@ -39,8 +42,13 @@ void* xCalloc(size_t nmemb, size_t size) { } void* xRealloc(void* ptr, size_t size) { - void* data = realloc(ptr, size); - if (!data && size > 0) { + if (!size) { + free(ptr); + return NULL; + } + void* data = realloc(ptr, size); // deepcode ignore MemoryLeakOnRealloc: this goes to fail() + if (!data) { + free(ptr); fail(); } return data; |