summaryrefslogtreecommitdiff
path: root/libbb/xfuncs.c
diff options
context:
space:
mode:
authorMatt Kraai2002-02-26 15:28:22 +0000
committerMatt Kraai2002-02-26 15:28:22 +0000
commita99b1943360e3efaa60b83ee9b54c8077fee5212 (patch)
tree127bb463a98a21464d51507cf4ac008cd8d9481d /libbb/xfuncs.c
parenteed9451cf6076a07b1768c3ca5ef21618b00b38d (diff)
downloadbusybox-a99b1943360e3efaa60b83ee9b54c8077fee5212.zip
busybox-a99b1943360e3efaa60b83ee9b54c8077fee5212.tar.gz
* libbb/xfuncs.c (xmalloc, xcalloc): Do not exit if a zero-length buffer is
requested. (xrealloc): Simplify.
Diffstat (limited to 'libbb/xfuncs.c')
-rw-r--r--libbb/xfuncs.c21
1 files changed, 5 insertions, 16 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 291bfaf..57c6980 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -30,26 +30,15 @@
extern void *xmalloc(size_t size)
{
void *ptr = malloc(size);
-
- if (!ptr)
+ if (ptr == NULL && size != 0)
error_msg_and_die(memory_exhausted);
return ptr;
}
-extern void *xrealloc(void *old, size_t size)
+extern void *xrealloc(void *ptr, size_t size)
{
- void *ptr;
-
- /* SuS2 says "If size is 0 and ptr is not a null pointer, the
- * object pointed to is freed." Do that here, in case realloc
- * returns a NULL, since we don't want to choke in that case. */
- if (size==0 && old) {
- free(old);
- return NULL;
- }
-
- ptr = realloc(old, size);
- if (!ptr)
+ ptr = realloc(ptr, size);
+ if (ptr == NULL && size != 0)
error_msg_and_die(memory_exhausted);
return ptr;
}
@@ -57,7 +46,7 @@ extern void *xrealloc(void *old, size_t size)
extern void *xcalloc(size_t nmemb, size_t size)
{
void *ptr = calloc(nmemb, size);
- if (!ptr)
+ if (ptr == NULL && nmemb != 0 && size != 0)
error_msg_and_die(memory_exhausted);
return ptr;
}