From 728a20eed7ec34e917d3a992b7777ceb9cee8404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Sun, 16 Sep 2012 21:41:35 +0200 Subject: Avoid calling libc realloc with a requested size of zero. --- realloc.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'realloc.c') 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; } -- cgit v1.2.1