aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2002-02-26 15:28:22 +0000
committerMatt Kraai <kraai@debian.org>2002-02-26 15:28:22 +0000
commita99b1943360e3efaa60b83ee9b54c8077fee5212 (patch)
tree127bb463a98a21464d51507cf4ac008cd8d9481d
parenteed9451cf6076a07b1768c3ca5ef21618b00b38d (diff)
downloadbusybox-w32-a99b1943360e3efaa60b83ee9b54c8077fee5212.tar.gz
busybox-w32-a99b1943360e3efaa60b83ee9b54c8077fee5212.tar.bz2
busybox-w32-a99b1943360e3efaa60b83ee9b54c8077fee5212.zip
* libbb/xfuncs.c (xmalloc, xcalloc): Do not exit if a zero-length buffer is
requested. (xrealloc): Simplify.
-rw-r--r--libbb/xfuncs.c21
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 @@
30extern void *xmalloc(size_t size) 30extern 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
39extern void *xrealloc(void *old, size_t size) 38extern 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)
57extern void *xcalloc(size_t nmemb, size_t size) 46extern 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}