$ gcc-13 --version
gcc-13 (Homebrew GCC 13.2.0) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: x86_64-apple-darwin22.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
-
-
Save osalbahr/796919bebe1d8960803c2a66f4d6ecfb to your computer and use it in GitHub Desktop.
$ cat uninitialized.c
#include <stdio.h>
int main()
{
int x;
for (int i = 0; i < 5; i++)
x += 1;
printf("%d\n", x);
}
$ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra -Wpedantic' make uninitialized
gcc-13 -std=c99 -Wall -Wextra -Wpedantic uninitialized.c -o uninitialized
$ # No warning
$ cat uninitialized.c
#include <stdio.h>
int main()
{
int x;
// for (int i = 0; i < 5; i++)
// x += 1;
printf("%d\n", x);
}
$ CC='gcc-13' CFLAGS='-std=c99 -Wall' make uninitialized
gcc-13 -std=c99 -Wall uninitialized.c -o uninitialized
uninitialized.c: In function 'main':
uninitialized.c:8:3: warning: 'x' is used uninitialized [-Wuninitialized]
8 | printf("%d\n", x);
| ^~~~~~~~~~~~~~~~~
uninitialized.c:5:7: note: 'x' was declared here
5 | int x;
| ^
Or.... adding one more non-warning flag???
$ CC='gcc-13' CFLAGS='-O' make uninitialized
gcc-13 -O uninitialized.c -o uninitialized
$ rm uninitialized
$ CC='gcc-13' CFLAGS='-Wall -O' make uninitialized
gcc-13 -Wall -O uninitialized.c -o uninitialized
uninitialized.c: In function 'main':
uninitialized.c:5:7: warning: 'x' is used uninitialized [-Wuninitialized]
5 | int x;
| ^
CC='clang' CFLAGS='-std=c99 -Wall' make uninitialized
clang -std=c99 -Wall uninitialized.c -o uninitialized
uninitialized.c:7:5: warning: variable 'x' is uninitialized when used here [-Wuninitialized]
x += 1;
^
uninitialized.c:5:8: note: initialize the variable 'x' to silence this warning
int x;
^
= 0
1 warning generated.
#include <stdio.h> | |
int main() | |
{ | |
int x; | |
for (int i = 0; i < 5; i++) | |
x += 1; | |
printf("%d\n", x); | |
} |
Here is the most surprising part, to me:
$ gcc-13 -Wuninitialized uninitialized.c
$ gcc-13 -Wuninitialized -O uninitialized.c
uninitialized.c: In function 'main':
uninitialized.c:5:7: warning: 'x' is used uninitialized [-Wuninitialized]
5 | int x;
| ^
GCC has the ability to warn the user about using the value of a uninitialized variable. Such value is undefined and it is never useful. It is not even useful as a random value, since it rarely is a random value. Unfortunately, detecting when the use of an uninitialized variable is equivalent, in the general case, to solving the halting problem. GCC tries to detect some instances by using the information gathered by optimisers and warns about them when the option -Wuninitialized is given in the command line. There are a number of perceived shortcomings in current implementation. First, it only works when optimisation is enabled through -O1, -O2 or -O3. Second, the set of false positives or negatives varies according to the optimisations enabled. This also causes high variability of the warnings reported when optimisations are added or modified between releases.
- Source: Better Uninitialized Warnings
Bug 18501 goes a very long time ago:
OK, this is a rabbit hole.... (gcc loop uninitialized value no warning)
why am I not getting an "used uninitialized" warning from gcc in this trivial example? - Stack Overflow
Why is there not any warning on a declaration without initialization in a for loop? - Stack Overflow
GCC does not warn about uninitialized variables UB if you add a single, unrelated if statement. : r/cpp
18501 – [11/12/13/14 Regression] Missing 'used uninitialized' warning (CCP)
Better Uninitialized Warnings
https://gcc.gnu.org/bugzilla/buglist.cgi?bug_id=30542%2C30575%2C30856%2C33327%2C36814%2C37148%2C38945%2C39113%2C40469%2C42724%2C42884%2C45493%2C46684%2C46853%2C47623%2C48414%2C48643%2C49971%2C56972%2C57629%2C58323%2C58890%2C59225%2C60444%2C69578%2C78203%2C80152%2C80787%2C81049%2C81329%2C82203%2C82810%2C87365%2C89202%2C89501%2C96368
this is the closest 82810 – missed uninitialized variable warning in for/while loop