[llvm-bugs] [Bug 49029] New: Use of uninitialized variable is not reported when followed by initialization in loop

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Feb 3 17:09:52 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=49029

            Bug ID: 49029
           Summary: Use of uninitialized variable is not reported when
                    followed by initialization in loop
           Product: clang
           Version: 11.0
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: aa1ronham at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

The following code does not produce any warnings when compiled with 'clang++
weird.cpp -Wall'

```
int main() {
    int x;
    for (int i = 0; i < 10; i++) {
        x += 1;
        x = 0;
    }
}
```
However, 'x' is uninitialized during the initial iteration through the loop. If
the 'x = 0;' line is removed:

```
int main() {
    int x;
    for (int i = 0; i < 10; i++) {
        x += 1;
    }
}
```

then a warning is produced when compiled with 'clang++ weird.cpp -Wall'

```
weird.cpp:4:9: warning: variable 'x' is uninitialized when used here
[-Wuninitialized]
        x += 1;
        ^
weird.cpp:2:10: note: initialize the variable 'x' to silence this warning
    int x;
         ^
          = 0
1 warning generated.
```

The statement 'x = 0;' occurs after the uninitialized use of x, so it should
not cause the warning to be suppressed.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210204/fa9023c9/attachment.html>


More information about the llvm-bugs mailing list