[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 --- Comment #8 from Andrew Pinski --- (In reply to Andrew D'Addesio from comment #7) > > This actually has gotten me curious. Would you have an idea/explanation > behind that 2 function call threshold @Andrew Pinski? Most likely it is due to jump threading optimization not happening if there are 2 calls while it is happening with 1. That is the growth of copying 1 call is reasonable while 2 is not when removing the extra jump. That is transforming: ``` int test(int x) { const unsigned char buf[32]; const struct mytype *ptr = if (x != 0) ptr = &(const struct mytype){ 43 }; foo(buf); #ifdef CALL_FOO_TWICE foo(buf); #endif return ptr->c; } ``` into something like: ``` int test(int x) { const unsigned char buf[32]; const struct mytype *ptr; if (x != 0) goto a; else goto b; a: { ptr = &(const struct mytype){ 43 }; } foo(buf); #ifdef CALL_FOO_TWICE foo(buf); #endif return ptr->c; b: foo(buf); #ifdef CALL_FOO_TWICE foo(buf); #endif return d.c; } ``` Where doing 2 copies of 2 calls is too expensive to be done.
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 --- Comment #7 from Andrew D'Addesio --- (In reply to Andrew Pinski from comment #6) > The warning is now included in GCC 12. > And this makes this a dup of bug 63272. > > *** This bug has been marked as a duplicate of bug 63272 *** Yep, the new warning is working nicely on my test case (comment #5) on GCC 14.0.1 on Fedora 40 x86-64: test.c: In function ‘test’: test.c:22:15: warning: dangling pointer ‘ptr’ to an unnamed temporary may be used [-Wdangling-pointer=] 22 | return ptr->c; |~~~^~~ test.c:15:37: note: unnamed temporary defined here 15 | ptr = &(const struct mytype){ 43 }; | ^ Though one thing to note -- (in addition to the above warning) it still generates that "strange" -Wuninitialized warning if and only if there are one or fewer calls to foo(): In function ‘test’, inlined from ‘main’ at test.c:27:12: test.c:22:15: warning: ‘.c’ is used uninitialized [-Wuninitialized] 22 | return ptr->c; |~~~^~~ ^ If foo() is called 2+ times then that warning disappears. It's not a bug per se as the "real" -Wdangling-pointer warning still gets displayed to the user (Ufe10 is probably GCC's representation of the variable that was never initialized). But I find it very peculiar that the threshold for silencing that -Wuninitialized warning is 2 function calls and not say 1 or 10. This actually has gotten me curious. Would you have an idea/explanation behind that 2 function call threshold @Andrew Pinski?
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Andrew Pinski changed: What|Removed |Added Resolution|--- |DUPLICATE Status|NEW |RESOLVED --- Comment #6 from Andrew Pinski --- The warning is now included in GCC 12. And this makes this a dup of bug 63272. *** This bug has been marked as a duplicate of bug 63272 ***
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Bug 89990 depends on bug 88058, which changed state. Bug 88058 Summary: gcc fails to detect use of out of scope variable ? https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88058 What|Removed |Added Status|NEW |RESOLVED Resolution|--- |DUPLICATE
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Andrew D'Addesio changed: What|Removed |Added CC||modchipv12 at gmail dot com --- Comment #5 from Andrew D'Addesio --- GCC already warns about this at compile time, but the warning sometimes doesn't appear (due to PR88058, as Andrew Pinski just mentioned). Plus, the warning is a bit confusing and could be reworded. For example, create the following files: foo.c: int foo(const unsigned char *buf) { (void)buf; /* unused parameter */ return 1; } test.c: int foo(const unsigned char *buf); struct mytype { char c; }; static struct mytype d = { 42 }; int test(int x) { const unsigned char buf[32]; const struct mytype *ptr = if (x != 0) ptr = &(const struct mytype){ 43 }; foo(buf); #ifdef CALL_FOO_TWICE foo(buf); #endif return ptr->c; } int main() { return test(1); /* returns 43 on GCC8, 0 on GCC9+ */ } Compiling with one foo() call gives us a warning: $ gcc -std=c99 -Wall -Wextra -pedantic -O1 -o test test.c foo.c test.c: In function ‘main’: test.c:26:15: warning: ‘.c’ is used uninitialized in this function [-Wuninitialized] 26 | return ptr->c; |~~~^~~ $ ./test $ echo $? 0 However, compiling with two foo() calls makes the warning disappear, for some reason: $ gcc -DCALL_FOO_TWICE -std=c99 -Wall -Wextra -pedantic -O1 -o test test.c foo.c $ ./test $ echo $? 0 My GCC version is 9.3.1 20200317 (Red Hat 9.3.1-1) on Fedora 31 x86-64.
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Martin Sebor changed: What|Removed |Added Status|UNCONFIRMED |NEW Last reconfirmed||2019-04-08 CC||msebor at gcc dot gnu.org Ever confirmed|0 |1 --- Comment #4 from Martin Sebor --- I agree that this would be a very useful enhancement. The value of a pointer becomes indeterminate after the lifetime of the object to which it points has ended. Even reading such a pointer is undefined, never mind dereferencing it. GCC could use that to issue helpful diagnostics even in absence of any evidence that the pointer is dereferenced, such as in the modified example below: int foo (mytype *ptr) { if (!ptr) { ptr = &(mytype) { }; } bar (ptr); // undefined } This applies not just to compound literals but to all other objects, including auto, allocated, and thread local storage.
[Bug c++/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Richard Biener changed: What|Removed |Added Keywords||diagnostic Component|c |c++ Severity|normal |enhancement
[Bug c/89990] request warning: Use of out of scope compound literals
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89990 Andrew Pinski changed: What|Removed |Added Depends on||88058 --- Comment #3 from Andrew Pinski --- PR 88058. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88058 [Bug 88058] gcc fails to detect use of out of scope variable ?