[Bug c/89990] request warning: Use of out of scope compound literals

2024-05-06 Thread modchipv12 at gmail dot com via Gcc-bugs
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

2020-03-30 Thread modchipv12 at gmail dot com
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.