goo/gmem.h |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

New commits:
commit 247119e3db4a19d57000f15d350165343b0e9092
Author: Even Rouault <[email protected]>
Date:   Mon Sep 13 22:52:50 2021 +0200

    greallocn(checkoverflow = true, free_p = true): if memory allocation fails, 
free the previous pointer to avoid memory leak

diff --git a/goo/gmem.h b/goo/gmem.h
index d104061b..4454a27a 100644
--- a/goo/gmem.h
+++ b/goo/gmem.h
@@ -168,7 +168,13 @@ inline void *greallocn(void *p, int count, int size, bool 
checkoverflow = false,
         std::abort();
     }
 
-    return grealloc(p, bytes, checkoverflow);
+    if (void *q = grealloc(p, bytes, checkoverflow)) {
+        return q;
+    }
+    if (free_p) {
+        gfree(p);
+    }
+    return nullptr;
 }
 
 inline void *greallocn_checkoverflow(void *p, int count, int size)

Reply via email to