Issue 81734
Summary [clang] False positive clang-analyzer-core.DivideZero with loop over container
Labels clang
Assignees
Reporter chrchr-github
    ~~~c++
#include <vector>
int f(const std::vector<int>& v) {
    if (v.empty())
 return 0;
    int h = 0;
    for (auto it = v.begin(); it != v.end(); ++it) {
        h = std::max(h, *it);
    }

    int x = 512;
 if (h < x)
        h = h * (x / h + 1);
    return h;
}
~~~
~~~
<source>:12:20: warning: Division by zero [clang-analyzer-core.DivideZero]
   12 |         h = h * (x / h + 1);
 |                  ~~^~~
<source>:3:9: note: Assuming the condition is false]
    3 |     if (v.empty())
      | ^~~~~~~~~
<source>:3:5: note: Taking false branch]
    3 |     if (v.empty())
      |     ^
<source>:5:5: note: 'h' initialized to 0
 5 |     int h = 0;
      |     ^~~~~
<source>:6:5: note: Loop condition is false. Execution continues on line 10
    6 |     for (auto it = v.begin(); it != v.end(); ++it) {
      |     ^
<source>:11:9: note: 'h' is < 'x'
   11 |     if (h < x)
      | ^
<source>:11:5: note: Taking true branch
   11 |     if (h < x)
 |     ^
<source>:12:20: note: Division by zero
   12 |         h = h * (x / h + 1);
      |                  ~~^~~
~~~
There could be division by zero if the vector holds only numbers <= 0. However, the diagnostic seems to imply that the vector might be empty, which is impossible.
https://godbolt.org/z/cbKh7axxd
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to