Clang can not detect runtime error of invalid float cast
| Bugzilla Link | 46095 |
| Resolution | INVALID |
| Resolved on | May 28, 2020 15:52 |
| Version | trunk |
| OS | Linux |
| CC | @DougGregor,@zygoloid |
Extended Description
This case test.cc
#include
int main(){ double n = 10e50; float m = (float)n; // Error: 10e50 can't be represented as a float. std::cout << "ok" << std::endl; return 0; }
$clang++-8 -w -fsanitize=float-cast-overflow test.cc ; ./a.out test.cc:5:22: runtime error: 1e+51 is outside the range of representable values of type 'float' ok
$clang++-9 -w -fsanitize=float-cast-overflow test.cc ; ./a.out ok
$clang++-10 -w -fsanitize=float-cast-overflow test.cc ; ./a.out ok
$clang++-trunk -w -fsanitize=float-cast-overflow test.cc ; ./a.out ok
My clang-trunk version is $clang++-trunk --version clang version 11.0.0 (https://github.com/llvm/llvm-project 210616bd38d589020b45f8cbbf9f9ef1296f2729) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /home/haoxin/compilers/llvm-clang/llvm-project/build/bin
This was an intentional change. Between Clang 8 and Clang 9, we changed the range of representable values of floating-point types from [-max, +max] to [-inf, +inf], so there is no longer any undefined behavior when converting between floating-point types in Clang.
This is documented at https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks:
""" -fsanitize=float-cast-overflow: Conversion to, from, or between floating-point types which would overflow the destination. Because the range of representable values for all floating-point types supported by Clang is [-inf, +inf], the only cases detected are conversions from floating point to integer types. """
Converting 1e51 from double to float is valid since Clang 9, and produces +inf. This is working as intended.