From: Paul Eggert Date: Thu, 24 Mar 2011 20:10:38 +0000 (-0700) Subject: xmalloc: Do not leak if underlying realloc is C99 compatible. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84a1c646b281107213dcaa481b97e0fcba2ec589;p=pspp xmalloc: Do not leak if underlying realloc is C99 compatible. * lib/xmalloc.c (xrealloc): If N is zero, call 'free' directly. This avoids a leak on C99-based systems. See . --- diff --git a/ChangeLog b/ChangeLog index 58f81959cb..c58e13c483 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2011-03-24 Paul Eggert + + xmalloc: Do not leak if underlying realloc is C99 compatible. + * lib/xmalloc.c (xrealloc): If N is zero, call 'free' directly. + This avoids a leak on C99-based systems. See + . + 2011-03-24 Eric Blake realloc: document portability problem diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 74a8614b6c..4589e7d74e 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -52,8 +52,16 @@ xmalloc (size_t n) void * xrealloc (void *p, size_t n) { + if (!n) + { + /* The GNU and C99 realloc behaviors disagree here. Act like + GNU, even if the underlying realloc is C99. */ + free (p); + return NULL; + } + p = realloc (p, n); - if (!p && n != 0) + if (!p) xalloc_die (); return p; }