Commit f5c7f3da authored by Eric Wong's avatar Eric Wong

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
parent 12d25311
...@@ -133,13 +133,14 @@ mpd_malloc void *xmalloc(size_t size) ...@@ -133,13 +133,14 @@ mpd_malloc void *xmalloc(size_t size)
mpd_malloc void *xrealloc(void *ptr, 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 /* some C libraries return NULL when size == 0,
* may be too extreme... (eric) */ * make sure we get a free()-able pointer (free(NULL)
assert((mpd_likely(size))); * 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)) if (mpd_unlikely(!ret))
FATAL("OOM: realloc\n"); FATAL("OOM: realloc\n");
return ret; return ret;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment