public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "guilherme.janczak at yandex dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/107838] spurious "may be used uninitialized" warning on variable initialized at the first iteration of a loop
Date: Wed, 11 Jan 2023 21:03:40 +0000	[thread overview]
Message-ID: <bug-107838-4-5QyWEDwxPj@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107838-4@http.gcc.gnu.org/bugzilla/>

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

Guilherme Janczak <guilherme.janczak at yandex dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |guilherme.janczak at yandex dot co
                   |                            |m

--- Comment #3 from Guilherme Janczak <guilherme.janczak at yandex dot com> ---
I just ran into the same bug with the following code:

/* rand32: return 32 bits of randomness */
static
unsigned long rand32(void)
{
        int rand(void);
        unsigned long rval;

        /*
         * There is modulo bias in this example if RAND_MAX isn't a power of 2
         * minus 1, but that is irrelevant for the bug report.
         *
         * C only guarantees RAND_MAX is at least 32767, that is, 15 bits.
         */
        rval = rand() & 0x7FFF;
        rval <<= 15;
        rval |= rand() & 0x7FFF;
        rval <<= 2;
        rval |= rand() & 0x03;
        return rval;

}

/* rand_hex: fill a buffer with random hex digits */
void rand_hex(unsigned char *buf, int len)
{
        const char *hex = "0123456789ABCDEF";
        int i;
        unsigned long r;

        for (i = 0; i < len; i++) {
                /* If we don't have any random bits in r, get some more. */
                if (i % 8 == 0)
                        r = rand32();

                /* Use 4 bits from the 32-bit integer at a time. */
                buf[i] = hex[r & 0x0F];
                r >>= 4;
        }
}

$ gcc -O -c -Wmaybe-uninitialized test.c
test.c: In function 'rand_hex':
test.c:37:19: warning: 'r' may be used uninitialized in this function
[-Wmaybe-uninitialized]
   37 |                 r >>= 4;
      |                 ~~^~~~~


Notice how the 1st usage of r doesn't cause the warning, but the 2nd one does.
I haven't tested GCC 13.0.0, I get this with the GCC 12.2.1 from Alpine Linux
and 11.2.0 from OpenBSD, here's their respective `gcc -v` outputs:
gcc version 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4)
gcc version 11.2.0 (GCC)

  parent reply	other threads:[~2023-01-11 21:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-23 16:25 [Bug tree-optimization/107838] New: " vincent-gcc at vinc17 dot net
2022-11-23 20:59 ` [Bug tree-optimization/107838] " rguenth at gcc dot gnu.org
2022-11-24 10:20 ` rguenth at gcc dot gnu.org
2023-01-11 21:03 ` guilherme.janczak at yandex dot com [this message]
2023-01-12  7:33 ` rguenth 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-107838-4-5QyWEDwxPj@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).