about summary refs log tree commit diff
path: root/lib/util/mallocvar.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/mallocvar.h')
-rw-r--r--lib/util/mallocvar.h19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/util/mallocvar.h b/lib/util/mallocvar.h
index a26d007b..1f2be127 100644
--- a/lib/util/mallocvar.h
+++ b/lib/util/mallocvar.h
@@ -57,11 +57,22 @@ static __inline__ void
 reallocProduct(void **      const blockP,
                unsigned int const factor1,
                unsigned int const factor2) {
+
+    void * const oldBlockP = *blockP;
+
+    void * newBlockP;
     
     if (UINT_MAX / factor2 < factor1) 
-        *blockP = NULL;
+        newBlockP = NULL;
     else 
-        *blockP = realloc(*blockP, factor1 * factor2); 
+        newBlockP = realloc(oldBlockP, factor1 * factor2); 
+
+    if (newBlockP)
+        *blockP = newBlockP;
+    else {
+        free(oldBlockP);
+        *blockP = NULL;
+    }
 }
 
 
@@ -72,10 +83,12 @@ reallocProduct(void **      const blockP,
     arrayName = array; \
 } while (0)
 
-#define REALLOCARRAY(arrayName, nElements) { \
+#define REALLOCARRAY(arrayName, nElements) do { \
     void * array; \
     array = arrayName; \
     reallocProduct(&array, nElements, sizeof(arrayName[0])); \
+    if (!array) \
+        free(arrayName); \
     arrayName = array; \
 } while (0)