diff options
author | Eric Wong <normalperson@yhbt.net> | 2006-09-07 19:26:51 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2006-09-07 19:26:51 +0000 |
commit | f5c7f3da31c948d70da63f593162f78db79e4833 (patch) | |
tree | e698168c690a01dffa7bc9ae8389b2eca447c77c | |
parent | 12d253118db6c83483ab477c5923663f18d80d35 (diff) | |
download | mpd-f5c7f3da31c948d70da63f593162f78db79e4833.tar.gz mpd-f5c7f3da31c948d70da63f593162f78db79e4833.tar.xz mpd-f5c7f3da31c948d70da63f593162f78db79e4833.zip |
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
Diffstat (limited to '')
-rw-r--r-- | src/utils.c | 11 |
1 files changed, 6 insertions, 5 deletions
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; |