From: Niels Möller Date: Sun, 16 Sep 2012 19:41:35 +0000 (+0200) Subject: Avoid calling libc realloc with a requested size of zero. X-Git-Tag: nettle_2.6_release_20130116~63 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=728a20eed7ec34e917d3a992b7777ceb9cee8404;p=thirdparty%2Fnettle.git Avoid calling libc realloc with a requested size of zero. --- diff --git a/ChangeLog b/ChangeLog index cdac8ae7..72dcf479 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2012-09-16 Niels Möller + * realloc.c (nettle_realloc): Only call libc realloc if length > + 0, otherwise call free. Fixes a small memory leak. + (nettle_xrealloc): Likewise. + * run-tests (test_program): Don't quote $EMULATOR; allow it to expand to program and arguments (e.g., valgrind). diff --git a/realloc.c b/realloc.c index f5684028..5c12a0b9 100644 --- a/realloc.c +++ b/realloc.c @@ -31,20 +31,32 @@ #include "realloc.h" +/* NOTE: Calling libc realloc with size == 0 is not required to + totally free the object, it is allowed to return a valid + pointer. */ void * nettle_realloc(void *ctx UNUSED, void *p, unsigned length) { - return realloc(p, length); + if (length > 0) + return realloc(p, length); + + free(p); + return NULL; } void * nettle_xrealloc(void *ctx UNUSED, void *p, unsigned length) { - void *n = realloc(p, length); - if (length && !n) + if (length > 0) { - fprintf(stderr, "Virtual memory exhausted.\n"); - abort(); + void *n = realloc(p, length); + if (!n) + { + fprintf(stderr, "Virtual memory exhausted.\n"); + abort(); + } + return n; } - return n; + free(p); + return NULL; }