diff mbox series

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

Message ID 20220616193529.56513-1-vishal.l.verma@intel.com (mailing list archive)
State Accepted
Commit bbb2cb56f08d95ecf2c7c047a33cc3dd64eb7fde
Headers show
Series [ndctl] util/wrapper.c: Fix gcc warning in xrealloc() | expand

Commit Message

Verma, Vishal L June 16, 2022, 7:35 p.m. UTC
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 <dan.j.williams@intel.com>
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
---
 util/wrapper.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)


base-commit: 3e17210345482ec9795f1046c766564d3b8a0795

Comments

Dan Williams June 16, 2022, 8:30 p.m. UTC | #1
Vishal Verma wrote:
> 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 <dan.j.williams@intel.com>
> Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>

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

Reviewed-by: Dan Williams <dan.j.williams@intel.com>
Verma, Vishal L June 16, 2022, 8:33 p.m. UTC | #2
On Thu, 2022-06-16 at 13:30 -0700, Dan Williams wrote:
> Vishal Verma wrote:
> > 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 <dan.j.williams@intel.com>
> > Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
> 
> 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).

> 
> Reviewed-by: Dan Williams <dan.j.williams@intel.com>

Thanks!
diff mbox series

Patch

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;
 }