public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug analyzer/110501] New: Invalid use-after-free / realloc
@ 2023-06-30 15:10 cheyenne.wills at gmail dot com
  2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: cheyenne.wills at gmail dot com @ 2023-06-30 15:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110501

            Bug ID: 110501
           Summary: Invalid use-after-free / realloc
           Product: gcc
           Version: 12.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: analyzer
          Assignee: dmalcolm at gcc dot gnu.org
          Reporter: cheyenne.wills at gmail dot com
  Target Milestone: ---

Created attachment 55433
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55433&action=edit
testcase.i

I've ran into a problem where gcc-12 (and later versions) is producing a false
positive on a use-after-free following a realloc.

The attached information obtained from a current gentoo system using gcc
(Gentoo 12.3.1_p20230526 p2) 12.3.1 20230526) 

gcc -v -save-temps -Wall -c testcase.c &> testcase.log


I've been able to duplicate the problem on godbolt.org using different versions
of gcc from gcc 12.1 through gcc master (I also tried various architectures,
x86_64, arm, etc.). godbolt's gcc 11 does work as expected.

The gist of the problem is:
---
 S->sp = realloc(p, size * 2);
 if (S->sp == NULL && size != 0) {
     free(p);    /* << is flagged as a use after free */
     return 0;
 }
---
However the following works:
---
 char *t;
 t = realloc(p, size * 2);
 if (t == NULL && size != 0) {
     free(p);   /* << is not flagged as a use after free */
     return 0;
 }
 S->sp = t;
---

The provided testcase contains 3 simple functions.  The function fail1 and
fail2  has code that shows the invalid use-after-free, while the function
succeeds has code that does not produce the use-after-free message.  The only
difference between the failed functions and the success that the success
function uses a stack based temporary variable to hold the result of the
realloc.


==
$ $ gcc -Wall -Wextra  -c testcase.c 
testcase.c: In function ‘fail1’:
testcase.c:10:9: warning: pointer ‘p’ may be used after ‘realloc’
[-Wuse-after-free]
   10 |         free(p); /* Is flagged as a use after free */
      |         ^~~~~~~
testcase.c:8:13: note: call to ‘realloc’ here
    8 |     S->sp = realloc(p, size*2);
      |             ^~~~~~~~~~~~~~~~~~
testcase.c: In function ‘fail2’:
testcase.c:20:9: warning: pointer ‘p’ may be used after ‘realloc’
[-Wuse-after-free]
   20 |         free(p); /* Is flagged as a use after free */
      |         ^~~~~~~
testcase.c:18:9: note: call to ‘realloc’ here
   18 |     t = realloc(p, size*2);
      |         ^~~~~~~~~~~~~~~~~~
$

==

The problem was originally discovered while building from openafs's master
branch (www.openafs.org) with a gcc-13 compiler. 

  src/external/heimdal/krb5/crypto.c:1157:9: error: pointer ‘p’ may be
      used after ‘realloc’ [-Werror=use-after-free]
   1157 |         free(p);
        |         ^~~~~~~
  src/external/heimdal/krb5/crypto.c:1155:20: note: call to ‘realloc’
      here
   1155 |     result->data = realloc(p, sz);
        |                    ^~~~~~~~~~~~~~

The failing code is part of an "external library" from the heimdal project.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-21  9:16 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-30 15:10 [Bug analyzer/110501] New: Invalid use-after-free / realloc cheyenne.wills at gmail dot com
2023-06-30 15:11 ` [Bug analyzer/110501] " cheyenne.wills at gmail dot com
2023-06-30 16:10 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc at -O0 pinskia at gcc dot gnu.org
2023-06-30 16:12 ` [Bug tree-optimization/110501] Invalid use-after-free / realloc with a store/load happening pinskia at gcc dot gnu.org
2023-06-30 16:16 ` cheyenne.wills at gmail dot com
2023-07-03  6:24 ` rguenth at gcc dot gnu.org
2023-07-06 20:49 ` cheyenne.wills at gmail dot com
2023-10-31 16:18 ` jhb at FreeBSD dot org
2024-03-10 14:03 ` [Bug tree-optimization/110501] [13/14 regression] Invalid -Wuse-after-free " sjames at gcc dot gnu.org
2024-04-04  7:39 ` rguenth at gcc dot gnu.org
2024-04-04  7:39 ` rguenth at gcc dot gnu.org
2024-05-21  9:16 ` [Bug tree-optimization/110501] [13/14/15 " jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).