summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen2001-06-28 21:22:19 +0000
committerEric Andersen2001-06-28 21:22:19 +0000
commit029b4a04221794101812407f5c60155bdfdd1481 (patch)
tree89a4cdbbe7166132ff8bf9559ce914c4ccec81dc
parentec23c4920f0c4ba6854a4fb62497fe9519f79361 (diff)
downloadbusybox-029b4a04221794101812407f5c60155bdfdd1481.zip
busybox-029b4a04221794101812407f5c60155bdfdd1481.tar.gz
Allow xrealloc to act as a free() when size=0, per SuS2.
-Erik
-rw-r--r--libbb/xfuncs.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index f3b294b..eb93bf1 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -44,7 +44,17 @@ extern void *xmalloc(size_t size)
extern void *xrealloc(void *old, size_t size)
{
- void *ptr = realloc(old, 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)
error_msg_and_die(memory_exhausted);
return ptr;