llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

False positive: floating point comparisons

Open llvmbot opened this issue 13 years ago • 5 comments

Bugzilla Link 15108
Version unspecified
OS All
Attachments Test case
Reporter LLVM Bugzilla Contributor
CC @belkadan

Extended Description

I upgraded to XCode 4.6 (4H127) and noticed static analyzer false positives relating to floating point comparisons. I've boiled this behavior down to the attached test case.

The real-world code inspiring this test case is admittedly gross, but I believe it to be correct: if a floating point value does not equal a constant in one expression, it will still not equal that constant in a subsequent expression. In other words, I believe floating-point comparisons to be deterministic, but the static analyzer appears to believe otherwise.

$ ccc-analyzer -v
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~148/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
$ clang -v
clang version 3.0 (tags/RELEASE_30/final)
Target: x86_64-apple-darwin12.2.0
Thread model: posix

llvmbot avatar Jan 29 '13 19:01 llvmbot

I don't have an old version of XCode handy, but I've analyzed this codebase before, and I don't recall seeing this particular issue. This could be due to unrelated changes (new interprocedural analysis features?), or it could be my memory.

Is there some other way for the static analyser to reason about this code using common subexpression elimination or similar? That would address this test case without needing specific knowledge of floats -- it only requires the more general knowledge that the second branch cannot be reached without also having taken the first branch, since they're both predicated on identical conditions.

llvmbot avatar Jan 29 '13 20:01 llvmbot

Meanwhile, a workaround is to set a boolean flag along one if-branch, and then assert that it's true in the next. It'll get optimized out in a Release build, and it'll let the analyzer know that any other path is infeasible.

belkadan avatar Jan 29 '13 19:01 belkadan

The analyzer currently doesn't reason about floats at all. A good first step would be to just keep a set of constant float constraints, the way we started with integers.

General support for floats tracked in rdar://problem/8904778.

belkadan avatar Jan 29 '13 19:01 belkadan

assigned to @tkremenek

llvmbot avatar Jan 29 '13 19:01 llvmbot

Bisected: the reported behavior no longer reproduces starting with https://github.com/llvm/llvm-project/commit/c019142a89b477cd247434c1d8f571662d26e19d.

Godbolt: https://godbolt.org/z/a77ceeYPa

  • Clang 13.0.0 (assertions): Reports Called C++ object pointer is uninitialized at line 18
  • Clang 14.0.0 (assertions): No longer reports the uninitialized pointer warning at line 18
  • Clang (assertions trunk): Reports only the unrelated leak warning at line 20

This is only an observed change in behavior. I have not checked whether this commit addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.

Prepared with assistance from Codex.

hongtaihu avatar Oct 31 '26 08:10 hongtaihu