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)