diff options
-rw-r--r-- | libbb/xfuncs.c | 21 |
1 files changed, 5 insertions, 16 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 291bfafd6..57c698079 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -30,26 +30,15 @@ | |||
30 | extern void *xmalloc(size_t size) | 30 | extern void *xmalloc(size_t size) |
31 | { | 31 | { |
32 | void *ptr = malloc(size); | 32 | void *ptr = malloc(size); |
33 | 33 | if (ptr == NULL && size != 0) | |
34 | if (!ptr) | ||
35 | error_msg_and_die(memory_exhausted); | 34 | error_msg_and_die(memory_exhausted); |
36 | return ptr; | 35 | return ptr; |
37 | } | 36 | } |
38 | 37 | ||
39 | extern void *xrealloc(void *old, size_t size) | 38 | extern void *xrealloc(void *ptr, size_t size) |
40 | { | 39 | { |
41 | void *ptr; | 40 | ptr = realloc(ptr, size); |
42 | 41 | if (ptr == NULL && size != 0) | |
43 | /* SuS2 says "If size is 0 and ptr is not a null pointer, the | ||
44 | * object pointed to is freed." Do that here, in case realloc | ||
45 | * returns a NULL, since we don't want to choke in that case. */ | ||
46 | if (size==0 && old) { | ||
47 | free(old); | ||
48 | return NULL; | ||
49 | } | ||
50 | |||
51 | ptr = realloc(old, size); | ||
52 | if (!ptr) | ||
53 | error_msg_and_die(memory_exhausted); | 42 | error_msg_and_die(memory_exhausted); |
54 | return ptr; | 43 | return ptr; |
55 | } | 44 | } |
@@ -57,7 +46,7 @@ extern void *xrealloc(void *old, size_t size) | |||
57 | extern void *xcalloc(size_t nmemb, size_t size) | 46 | extern void *xcalloc(size_t nmemb, size_t size) |
58 | { | 47 | { |
59 | void *ptr = calloc(nmemb, size); | 48 | void *ptr = calloc(nmemb, size); |
60 | if (!ptr) | 49 | if (ptr == NULL && nmemb != 0 && size != 0) |
61 | error_msg_and_die(memory_exhausted); | 50 | error_msg_and_die(memory_exhausted); |
62 | return ptr; | 51 | return ptr; |
63 | } | 52 | } |