Skip to content

Instantly share code, notes, and snippets.

@osalbahr
Last active September 11, 2023 18:10
Show Gist options
  • Save osalbahr/796919bebe1d8960803c2a66f4d6ecfb to your computer and use it in GitHub Desktop.
Save osalbahr/796919bebe1d8960803c2a66f4d6ecfb to your computer and use it in GitHub Desktop.
`gcc-13` uninitialized value lack of warning after a loop?
$ 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

With gcc (GNU)

$ 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

Commenting-out the loop

$ 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;
      |       ^
$ 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;
      |       ^

With clang

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);
}
@osalbahr
Copy link
Author

osalbahr commented Sep 11, 2023

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.


Bug 18501 goes a very long time ago:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment