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 $