[llvm-bugs] [Bug 46710] Clang optimizer leads to wrong if-clause result

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Jul 14 10:41:15 PDT 2020


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

David Blaikie <dblaikie at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|NEW                         |RESOLVED
                 CC|                            |dblaikie at gmail.com

--- Comment #1 from David Blaikie <dblaikie at gmail.com> ---
> All of these may drive programmers' attention to undefined behaviors and they may spend a lot of time looking at their codes trying to find out any of those, which actually is not an undefined behavior.

But this is undefined behavior - integer overflow is UB in C++.

Clang's Undefined Behavior Sanitizer detects this:

$ cat > test.cpp
#include <iostream>

int main()
{
    int a; // Enter 2000000000 (2*10^9)
    int b; // Enter 1000000000 (10^9)
    std::cin >> a >> b;
    if (a + b >= a)
        std::cout << "Yes" << std::endl;
    else
        std::cout << "No" << std::endl;
}
$ clang++ -fsanitize=undefined test.cpp -g
$ echo "2000000000 1000000000" | ./a.out
test.cpp:9:11: runtime error: signed integer overflow: 2000000000 + 1000000000
cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.cpp:9:11 in 
No

-- 
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/20200714/1f00f84d/attachment-0001.html>


More information about the llvm-bugs mailing list