[llvm-bugs] [Bug 39675] New: Undefined behavior inside constexpr function

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Nov 15 05:06:37 PST 2018


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

            Bug ID: 39675
           Summary: Undefined behavior inside constexpr function
           Product: clang
           Version: trunk
          Hardware: Other
                OS: other
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: oleksandr.yefremov at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

Bug summary:
I create two functions a() and b().
Both are very similar and both invoke undefined behavior using integer
overflow.
clang compiler is able to optimize both functions (using -03 flag) up to single
constant.
Except constant is different in both cases! This is still acceptable because of
UB.

C++ forbids to invoke UB inside constexpr functions.
When making function a() constexpr compiler fails to compile code as expected
while it continue to compile very similar function b() and evaluates wrong
result.

Expected behavior:
compiler must not compile function b() when it is marked as constexpr because
of UB inside this function..

Sample code is available in compiler explorer at https://godbolt.org/z/tWXnnd

Sample code:

//constexpr
int a()
{
    int i = (1 << 30) + ((1 << 30) - 1);
    return i*2/2;
}

constexpr
int f(int i)
{
    return i*2/2;
}

constexpr
int b()
{
    int i = (1 << 30) + ((1 << 30) - 1);
    return f(i);
}

-- 
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/20181115/4b5e4117/attachment.html>


More information about the llvm-bugs mailing list