public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "modchipv12 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/89990] request warning: Use of out of scope compound literals
Date: Tue, 31 Mar 2020 02:34:12 +0000	[thread overview]
Message-ID: <bug-89990-4-jwbe3BYcY2@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-89990-4@http.gcc.gnu.org/bugzilla/>

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.

       reply	other threads:[~2020-03-31  2:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-89990-4@http.gcc.gnu.org/bugzilla/>
2020-03-31  2:34 ` modchipv12 at gmail dot com [this message]
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

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-89990-4-jwbe3BYcY2@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).