From dc6d5b2a41f12d6572e6db714562050f241e732f Mon Sep 17 00:00:00 2001
From: Stefan Bellon <sbellon@sbellon.de>
Date: Sat, 22 Dec 2001 18:47:58 +0000
Subject: [PATCH] fixed realloc not working correctly with M_GUARD

---
 util/ChangeLog |  5 +++++
 util/memory.c  | 30 +++++++++++++++++-------------
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/util/ChangeLog b/util/ChangeLog
index 6572bbacd..c1fa8d1bf 100644
--- a/util/ChangeLog
+++ b/util/ChangeLog
@@ -1,3 +1,8 @@
+2001-12-22  Stefan Bellon  <sbellon@sbellon.de>
+
+	* memory.c (realloc): Fixed realloc not working when M_GUARD is
+	defined and first parameter is NULL.
+
 2001-12-22  Timo Schulz <ts@winpt.org>
 
         * fileutil.c (is_file_compressed): New.
diff --git a/util/memory.c b/util/memory.c
index 76368cd97..fef2acc82 100644
--- a/util/memory.c
+++ b/util/memory.c
@@ -475,23 +475,26 @@ FNAME(alloc_secure_clear)( size_t n FNAMEPRT)
 void *
 FNAME(realloc)( void *a, size_t n FNAMEPRT )
 {
+    void *b;
+
   #ifdef M_GUARD
-    unsigned char *p = a;
-    void *b;
-    size_t len = m_size(a);
+    if( a ) {
+        unsigned char *p = a;
+        size_t len = m_size(a);
 
-    if( len >= n ) /* we don't shrink for now */
-	return a;
-    if( p[-1] == MAGIC_SEC_BYTE )
-	b = FNAME(alloc_secure_clear)(n FNAMEARG);
+        if( len >= n ) /* we don't shrink for now */
+            return a;
+        if( p[-1] == MAGIC_SEC_BYTE )
+            b = FNAME(alloc_secure_clear)(n FNAMEARG);
+        else
+            b = FNAME(alloc_clear)(n FNAMEARG);
+        FNAME(check)(NULL FNAMEARG);
+        memcpy(b, a, len );
+        FNAME(free)(p FNAMEARG);
+    }
     else
-	b = FNAME(alloc_clear)(n FNAMEARG);
-    FNAME(check)(NULL FNAMEARG);
-    memcpy(b, a, len );
-    FNAME(free)(p FNAMEARG);
+        b = FNAME(alloc)(n);
   #else
-    void *b;
-
     if( m_is_secure(a) ) {
 	if( !(b = secmem_realloc( a, n )) )
 	    out_of_core(n,1);
@@ -501,6 +504,7 @@ FNAME(realloc)( void *a, size_t n FNAMEPRT )
 	    out_of_core(n,0);
     }
   #endif
+
     return b;
 }