Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang doesn't warn about uninitialized stack variable

Had a bug in this code:

bool x;
    
for(const auto a : b)
{
    x = x && a.b;
}

x should have initialized to true.

I'd like to raise a compiler error that the bool wasn't explicitly initialized, but I cannot find a warning which covers this. We have the obvious flags:

-Wall \
-pedantic \
-Werror=uninitialized \ 

Is there another Clang warning that would catch this and other uninitialized variables?

like image 473
mezamorphic Avatar asked Oct 19 '25 13:10

mezamorphic


1 Answers

For this particular issue, compile with -Werror=conditional-uninitialized and you'll get a clear warning, turned into an error:

<source>:10:13: error: variable 'x' may be uninitialized when used here [-Werror,-Wconditional-uninitialized]
   10 |         x = x && a.b;
      |             ^
<source>:7:11: note: initialize the variable 'x' to silence this warning
    7 |     bool x;
      |           ^
      |            = false
like image 96
Ted Lyngmo Avatar answered Oct 21 '25 03:10

Ted Lyngmo