From f5c7f3da31c948d70da63f593162f78db79e4833 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 7 Sep 2006 19:26:51 +0000 Subject: utils.c: fix xrealloc (based on suggested patch by Jan-Benedict Glaw): > While hacking mpd, I noticed that an assert()ion in xrealloc is wrong. > A null size is perfectly legal, so we shouldn't assert on that. Since some C libraries return NULL when size == 0, we'll make sure we get a free()-able pointer since some of those C libraries also barf on free(NULL). git-svn-id: https://svn.musicpd.org/mpd/trunk@4740 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/utils.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 60fa3f135..919553d3e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -133,13 +133,14 @@ mpd_malloc void *xmalloc(size_t size) mpd_malloc void *xrealloc(void *ptr, size_t size) { - void *ret; + void *ret = realloc(ptr, size); - /* hmm... realloc to 0 isn't uncommon..., is it? this check - * may be too extreme... (eric) */ - assert((mpd_likely(size))); + /* some C libraries return NULL when size == 0, + * make sure we get a free()-able pointer (free(NULL) + * doesn't work with all C libraries, either) */ + if (mpd_unlikely(!ret && !size)) + ret = realloc(ptr, 1); - ret = realloc(ptr, size); if (mpd_unlikely(!ret)) FATAL("OOM: realloc\n"); return ret; -- cgit v1.2.3