https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111063
Bug ID: 111063 Summary: [UBSAN] Implement "-inf is outside the range of representable values of type 'unsigned long'" to be on par with Clang Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: sanitizer Assignee: unassigned at gcc dot gnu.org Reporter: burnus at gcc dot gnu.org CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org, jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org Target Milestone: --- Jonathan Wakely wrote in bug 110860 comment 10: -----------<cut>------------------------- GCC's ubsan doesn't seem to diagnose this rule: "A prvalue of a floating-point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type." There's no ubsan diagnostic for: unsigned long i = -HUGE_VAL; So it doesn't complain about adding log10(0) to a size_t. -----------<cut>------------------------- That diagnostic exists in Clang as reported by Paul Dreik in bug 110860 comment 8: -----------<cut>------------------------- When 0 is passed, log10 returns -inf, which can not be converted to an integer. I had a bit of problem to reproduce this with gcc, but it worked with clang. The following program: #include <cstdio> #include <format> #include <string_view> int main(int argc, char* argv[]) { [[maybe_unused]] auto x = std::format("{:.292f}", 0.f); } causes the following output when compiled with clang-16 using -fsanitize=undefined -fno-sanitize-recover=all -g -O0: /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/format:1496:15: runtime error: -inf is outside the range of representable values of type 'unsigned long' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/format:1496:15 in Link to reproducer (expected to go stale quick, had to use clang trunk to get a sufficiently new libstdc++): https://godbolt.org/z/8aGf7YGWs