public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/89990] request warning: Use of out of scope compound literals
       [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
@ 2020-03-31  2:34 ` modchipv12 at gmail dot com
  2024-04-09  7:32 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: modchipv12 at gmail dot com @ 2020-03-31  2:34 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew D'Addesio <modchipv12 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |modchipv12 at gmail dot com

--- Comment #5 from Andrew D'Addesio <modchipv12 at gmail dot com> ---
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 = &d;

        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: ‘<U2cf0>.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.

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

* [Bug c/89990] request warning: Use of out of scope compound literals
       [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
  2020-03-31  2:34 ` [Bug c/89990] request warning: Use of out of scope compound literals modchipv12 at gmail dot com
@ 2024-04-09  7:32 ` pinskia at gcc dot gnu.org
  2024-04-09  7:33 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-09  7:32 UTC (permalink / raw)
  To: gcc-bugs

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

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

* [Bug c/89990] request warning: Use of out of scope compound literals
       [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
  2020-03-31  2:34 ` [Bug c/89990] request warning: Use of out of scope compound literals modchipv12 at gmail dot com
  2024-04-09  7:32 ` pinskia at gcc dot gnu.org
@ 2024-04-09  7:33 ` pinskia at gcc dot gnu.org
  2024-05-07  2:42 ` modchipv12 at gmail dot com
  2024-05-07  4:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-04-09  7:33 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|NEW                         |RESOLVED

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
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 ***

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

* [Bug c/89990] request warning: Use of out of scope compound literals
       [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2024-04-09  7:33 ` pinskia at gcc dot gnu.org
@ 2024-05-07  2:42 ` modchipv12 at gmail dot com
  2024-05-07  4:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: modchipv12 at gmail dot com @ 2024-05-07  2:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew D'Addesio <modchipv12 at gmail dot com> ---
(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: ‘<Ufe10>.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?

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

* [Bug c/89990] request warning: Use of out of scope compound literals
       [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
                   ` (3 preceding siblings ...)
  2024-05-07  2:42 ` modchipv12 at gmail dot com
@ 2024-05-07  4:55 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-05-07  4:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(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 = &d;

        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.

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

end of thread, other threads:[~2024-05-07  4:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
2020-03-31  2:34 ` [Bug c/89990] request warning: Use of out of scope compound literals modchipv12 at gmail dot com
2024-04-09  7:32 ` pinskia at gcc dot gnu.org
2024-04-09  7:33 ` pinskia at gcc dot gnu.org
2024-05-07  2:42 ` modchipv12 at gmail dot com
2024-05-07  4:55 ` pinskia at gcc dot gnu.org

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