The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: "Justin M. Forbes" <jforbes@fedoraproject.org>
Cc: linux-kernel@vger.kernel.org, jmforbes@linuxtx.org
Subject: Re: [PATCH] Fix for gcc12 compile issues in ubcmd-util.h
Date: Sat, 29 Jan 2022 09:53:48 +0100	[thread overview]
Message-ID: <20220129085348.GT2646553@tucnak> (raw)
In-Reply-To: <20220129010215.781601-1-jforbes@fedoraproject.org>

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


      reply	other threads:[~2022-01-29  8:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-29  1:02 [PATCH] Fix for gcc12 compile issues in ubcmd-util.h Justin M. Forbes
2022-01-29  8:53 ` Jakub Jelinek [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220129085348.GT2646553@tucnak \
    --to=jakub@redhat.com \
    --cc=jforbes@fedoraproject.org \
    --cc=jmforbes@linuxtx.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox