[PATCH] Fix for gcc12 compile issues in ubcmd-util.h

Justin M. Forbes posted 1 patch 4 years, 5 months ago
tools/lib/subcmd/subcmd-util.h | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)
[PATCH] Fix for gcc12 compile issues in ubcmd-util.h
Posted by Justin M. Forbes 4 years, 5 months ago
While the current code builds fine with gcc 11, it does not with gcc 12,
resulting in:

In file included from help.c:12:
In function 'xrealloc',
    inlined from 'add_cmdname' at help.c:24:2:
subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free]
   56 |                 ret = realloc(ptr, size);
      |                       ^~~~~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to 'realloc' here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~
subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free]
   58 |                         ret = realloc(ptr, 1);
      |                               ^~~~~~~~~~~~~~~
subcmd-util.h:52:21: note: call to 'realloc' here
   52 |         void *ret = realloc(ptr, size);
      |                     ^~~~~~~~~~~~~~~~~~

The was mentioned in upstream gcc bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104069 where it was
determined that gcc was correct and the kernel needed to change.  This
fixes that use-after-free and makes things build again.

Signed-off-by: Justin M. Forbes <jforbes@fedoraproject.org>
Cc: Jakub Jelinek <jakub@redhat.com>

---
 tools/lib/subcmd/subcmd-util.h | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/tools/lib/subcmd/subcmd-util.h b/tools/lib/subcmd/subcmd-util.h
index 794a375dad36..7009fc176636 100644
--- a/tools/lib/subcmd/subcmd-util.h
+++ b/tools/lib/subcmd/subcmd-util.h
@@ -49,13 +49,12 @@ static NORETURN inline void die(const char *err, ...)

 static inline void *xrealloc(void *ptr, size_t size)
 {
-	void *ret = realloc(ptr, size);
-	if (!ret && !size)
-		ret = realloc(ptr, 1);
+	void *ret;
+	if (!size)
+		size = 1;
+	ret = realloc(ptr, size);
 	if (!ret) {
 		ret = realloc(ptr, size);
-		if (!ret && !size)
-			ret = realloc(ptr, 1);
 		if (!ret)
 			die("Out of memory, realloc failed");
 	}
-- 
2.34.1

Re: [PATCH] Fix for gcc12 compile issues in ubcmd-util.h
Posted by Jakub Jelinek 4 years, 5 months ago
On Fri, Jan 28, 2022 at 07:02:14PM -0600, Justin M. Forbes wrote:
> While the current code builds fine with gcc 11, it does not with gcc 12,
> resulting in:
> 
> In file included from help.c:12:
> In function 'xrealloc',
>     inlined from 'add_cmdname' at help.c:24:2:
> subcmd-util.h:56:23: error: pointer may be used after 'realloc' [-Werror=use-after-free]
>    56 |                 ret = realloc(ptr, size);
>       |                       ^~~~~~~~~~~~~~~~~~
> subcmd-util.h:52:21: note: call to 'realloc' here
>    52 |         void *ret = realloc(ptr, size);
>       |                     ^~~~~~~~~~~~~~~~~~
> subcmd-util.h:58:31: error: pointer may be used after 'realloc' [-Werror=use-after-free]
>    58 |                         ret = realloc(ptr, 1);
>       |                               ^~~~~~~~~~~~~~~
> subcmd-util.h:52:21: note: call to 'realloc' here
>    52 |         void *ret = realloc(ptr, size);
>       |                     ^~~~~~~~~~~~~~~~~~
> 
> The was mentioned in upstream gcc bug
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104069 where it was
> determined that gcc was correct and the kernel needed to change.

This is due to the different behaviors of realloc when size is 0 as
described in http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_400
and as the code explicitly tries to cope with !size, there is at least
theoretical chance that it is called with size 0.
In glibc/AIX, for non-NULL ptr realloc(ptr, 0) will free(ptr) and return
NULL, so the code as written in that case will in
	void *ret = realloc(ptr, 0);
	if (!ret && !0)
		ret = realloc(ptr, 1);
effectively
	free(ptr);
	ret = realloc(ptr, 1);
and so try to reallocate freed memory.  On BSD libcs it will work properly
though.

	Jakub