diff options
author | Niels Möller <nisse@lysator.liu.se> | 2012-09-16 21:41:35 +0200 |
---|---|---|
committer | Niels Möller <nisse@lysator.liu.se> | 2012-09-16 21:41:35 +0200 |
commit | 728a20eed7ec34e917d3a992b7777ceb9cee8404 (patch) | |
tree | 66176e99492b4f184253e686a09aa2a9876f6921 /realloc.c | |
parent | 3a73862a5229bc53142b19a1608d9b5125908eb4 (diff) | |
download | nettle-728a20eed7ec34e917d3a992b7777ceb9cee8404.tar.gz |
Avoid calling libc realloc with a requested size of zero.
Diffstat (limited to 'realloc.c')
-rw-r--r-- | realloc.c | 24 |
1 files changed, 18 insertions, 6 deletions
@@ -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; } |