]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Avoid calling libc realloc with a requested size of zero.
authorNiels Möller <nisse@lysator.liu.se>
Sun, 16 Sep 2012 19:41:35 +0000 (21:41 +0200)
committerNiels Möller <nisse@lysator.liu.se>
Sun, 16 Sep 2012 19:41:35 +0000 (21:41 +0200)
ChangeLog
realloc.c

index cdac8ae7e972237831703fb8aa9e4a80b0d2b44f..72dcf47933bde0c6956eec8c3e23c9e132d67749 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2012-09-16  Niels Möller  <nisse@lysator.liu.se>
 
+       * 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).
 
index f568402883fd62de800388b5a37b3c94a36437d2..5c12a0b9c61839d6f2947d51e1e2749aa75c43d6 100644 (file)
--- a/realloc.c
+++ b/realloc.c
 
 #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;
 }