public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "mario at klebsch dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/112543] New: warning pointer used after free() for printf("%p")
Date: Wed, 15 Nov 2023 10:53:58 +0000	[thread overview]
Message-ID: <bug-112543-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112543
           Summary: warning pointer used after free() for printf("%p")
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mario at klebsch dot de
  Target Milestone: ---

Created attachment 56592
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=56592&action=edit
main.c preprocessed by gcc

Hello,

Compiling the following program with -Wall gives a use-after-free warning:


----8<--------8<--------8<--------8<--------8<--------8<----
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        char* p = (char*)malloc(5);
        printf("allocated %p\n", p);
        free(p);
        printf("freed %p\n", p);
}
----8<--------8<--------8<--------8<--------8<--------8<----

Compiling with gcc gives this output:
mkl@C707 ~/tmp/g++-bug $ gcc -Wall main.c  -o main
main.c: In function ‘main’:
main.c:9:9: warning: pointer ‘p’ used after ‘free’ [-Wuse-after-free]
    9 |         printf("freed %p\n", p);
      |         ^~~~~~~~~~~~~~~~~~~~~~~
main.c:8:9: note: call to ‘free’ here
    8 |         free(p);
      |         ^~~~~~~
mkl@C707 ~/tmp/g++-bug $ gcc --version
gcc (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mkl@C707 ~/tmp/g++-bug $ 

Compiling with g++:
mkl@C707 ~/tmp/g++-bug $ g++ -Wall main.cpp  -o main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:15: warning: pointer ‘p’ used after ‘void free(void*)’
[-Wuse-after-free]
    9 |         printf("freed %p\n", p);
      |         ~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:8:13: note: call to ‘void free(void*)’ here
    8 |         free(p);
      |         ~~~~^~~
mkl@C707 ~/tmp/g++-bug $ g++ --version
g++ (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

mkl@C707 ~/tmp/g++-bug $

             reply	other threads:[~2023-11-15 10:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-15 10:53 mario at klebsch dot de [this message]
2023-11-15 11:02 ` [Bug c/112543] " schwab@linux-m68k.org
2023-11-15 12:00 ` rguenth at gcc dot gnu.org
2023-11-15 22:36 ` xry111 at gcc dot gnu.org

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=bug-112543-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).