public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/106578] New: spurious -Wuse-after-free=2 after conditional free()
@ 2022-08-10 14:16 gcc.gnu.org at aydos dot de
  2022-08-10 14:22 ` [Bug middle-end/106578] " gcc.gnu.org at aydos dot de
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: gcc.gnu.org at aydos dot de @ 2022-08-10 14:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106578
           Summary: spurious -Wuse-after-free=2 after conditional free()
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gcc.gnu.org at aydos dot de
  Target Milestone: ---

I am new to GCC's components, so feel free to rename the headers if I
classified the bug wrong.

The following code throws the `use-after-free` error:

```
#include <stdlib.h>

void *buf = NULL;
size_t buf_size = 0;

int main() {
    void *tmp = buf;

    buf = realloc(tmp, 10);

    // Realloc returns a pointer to the new object, or a null pointer if the
    // new object could not be allocated. Free the original pointer
    // to avoid memory leak in latter case.
    if (!buf)
        free(tmp);
}
```
```
$ gcc -Wall -Wuse-after-free=2 main.c
main.c: In function ‘main’:
main.c:15:17: warning: pointer ‘tmp’ may be used after ‘realloc’
[-Wuse-after-free]
   15 |                 free(tmp);
      |                 ^~~~~~~~~
main.c:9:15: note: call to ‘realloc’ here
    9 |         buf = realloc(tmp, 10);
      |               ^~~~~~~~~~~~~~~~
```

A workaround is to `realloc` directly `buf` and use `tmp` for the return value
of `realloc`:

```
#include <stdlib.h>

void *buf = NULL;
size_t buf_size = 0;

int main() {
    void *tmp = buf;

    tmp = realloc(buf, 10);

    if (!tmp)
        free(buf);
}
```

>From what I saw in other bugs this one may also be related to the meta-bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104075.

Can someone confirm this case?

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

end of thread, other threads:[~2022-08-11  9:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10 14:16 [Bug middle-end/106578] New: spurious -Wuse-after-free=2 after conditional free() gcc.gnu.org at aydos dot de
2022-08-10 14:22 ` [Bug middle-end/106578] " gcc.gnu.org at aydos dot de
2022-08-10 14:46 ` rguenth at gcc dot gnu.org
2022-08-10 16:04 ` [Bug middle-end/106578] spurious -Wuse-after-free=2 after conditional free() when not optimizing gcc.gnu.org at aydos dot de
2022-08-11  9:30 ` gcc.gnu.org at aydos dot de

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).