diff options
-rw-r--r-- | libbb/xfuncs.c | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index f3b294be2..eb93bf139 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -44,7 +44,17 @@ extern void *xmalloc(size_t size) | |||
44 | 44 | ||
45 | extern void *xrealloc(void *old, size_t size) | 45 | extern void *xrealloc(void *old, size_t size) |
46 | { | 46 | { |
47 | void *ptr = realloc(old, size); | 47 | void *ptr; |
48 | |||
49 | /* SuS2 says "If size is 0 and ptr is not a null pointer, the | ||
50 | * object pointed to is freed." Do that here, in case realloc | ||
51 | * returns a NULL, since we don't want to choke in that case. */ | ||
52 | if (size==0 && old) { | ||
53 | free(old); | ||
54 | return NULL; | ||
55 | } | ||
56 | |||
57 | ptr = realloc(old, size); | ||
48 | if (!ptr) | 58 | if (!ptr) |
49 | error_msg_and_die(memory_exhausted); | 59 | error_msg_and_die(memory_exhausted); |
50 | return ptr; | 60 | return ptr; |