Skip to contentSkip to conversation

util/wrapper.c: Fix gcc warning in xrealloc()

PATCH In review Vishal Verma 2 participants 3 messages Jun 16, 2022 libnvdimm: non-volatile memory device

lore
Share & tools
lore

Vishal Verma

author maintainer
#0 lore raw

PATCH 1/1 util/wrapper.c: Fix gcc warning in xrealloc()

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.

---
 util/wrapper.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
util/wrapper.c +9 -9
index 026a54f..6adfde6 100644
@@ -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

Signature · 1 line
2.36.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Vishal Verma

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]

Quoted 7 lines
     34 |                         ret = realloc(ptr, 1);
        |                               ^~~~~~~~~~~~~~~

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

Looks like a faithful reproduction of what upstream git did minus the memory_limit_check() infra, but we can think about that later.

author maintainer
#2 lore raw
Quoted 11 lines · Dan Williams
Quoted 10 lines · Vishal Verma

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); |                               ^~~~~~~~~~~~~~~

Quoted 8 lines
Quoted 5 lines

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

Looks like a faithful reproduction of what upstream git did minus the memory_limit_check() infra, but we can think about that later.

And minus the xmalloc() stuff, but I don't think we /need/ that (yet).

Quoted 2 lines

Thanks!