A GCC update (12.1.1) now produces a warning in the xrealloc() wrapper
(originally copied from git, and used in strbuf operations):

  ../util/wrapper.c: In function ‘xrealloc’:
  ../util/wrapper.c:34:31: warning: pointer ‘ptr’ may be used after ‘realloc’ 
[-Wuse-after-free]
     34 |                         ret = realloc(ptr, 1);
        |                               ^~~~~~~~~~~~~~~

Pull in an updated definition for xrealloc() from the git project to fix this.

Cc: Dan Williams <[email protected]>
Signed-off-by: Vishal Verma <[email protected]>
---
 util/wrapper.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/util/wrapper.c b/util/wrapper.c
index 026a54f..6adfde6 100644
--- a/util/wrapper.c
+++ b/util/wrapper.c
@@ -25,15 +25,15 @@ char *xstrdup(const char *str)
 
 void *xrealloc(void *ptr, size_t size)
 {
-       void *ret = realloc(ptr, size);
-       if (!ret && !size)
-               ret = realloc(ptr, 1);
-       if (!ret) {
-               ret = realloc(ptr, size);
-               if (!ret && !size)
-                       ret = realloc(ptr, 1);
-               if (!ret)
-                       die("Out of memory, realloc failed");
+       void *ret;
+
+       if (!size) {
+               free(ptr);
+               return malloc(1);
        }
+
+       ret = realloc(ptr, size);
+       if (!ret)
+               die("Out of memory, realloc failed");
        return ret;
 }

base-commit: 3e17210345482ec9795f1046c766564d3b8a0795
-- 
2.36.1


Reply via email to