[llvm-bugs] [Bug 43686] New: clang handles differently signed overflow depending on variable constness and its storage

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Oct 16 04:36:35 PDT 2019


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

            Bug ID: 43686
           Summary: clang handles differently signed overflow depending on
                    variable constness and its storage
           Product: new-bugs
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: new bugs
          Assignee: unassignedbugs at nondot.org
          Reporter: ki.stfu at gmail.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org

Godbolt: https://godbolt.org/z/2uqkzB

The first one, with i32_max int on stack prints "is 0" and returns 0:
```
int int_test() {
    int i32_max = std::numeric_limits<int>::max();
    int res = i32_max + 3;

    printf("res %d < i32_max %d is %d\n", res, i32_max, (int)(res < i32_max)); 
// ... is 0
    return (int)(res < i32_max);  // returns 0
}
-----------------------------------
int_test(): # @int_test()
  push rax
  mov edi, offset .L.str
  mov esi, -2147483646
  mov edx, 2147483647
  xor ecx, ecx
  xor eax, eax
  call printf
  xor eax, eax
  pop rcx
  ret
```

The second function, with i32_max const int on stack prints "is 1" and returns
1:
```
int const_int_test() {
    const int i32_max = std::numeric_limits<int>::max();
    int res = i32_max + 3;

    printf("res %d < i32_max %d is %d\n", res, i32_max, (int)(res < i32_max)); 
// ... is 1
    return (int)(res < i32_max);  // returns 1
}
-----------------------------------
const_int_test(): # @const_int_test()
  push rax
  mov edi, offset .L.str
  mov esi, -2147483646
  mov edx, 2147483647
  mov ecx, 1
  xor eax, eax
  call printf
  mov eax, 1
  pop rcx
  ret
```

And in the last case, with i32_max static int, it prints "is 0" and returns 1:
```
int static_int_test() {
    static int i32_max = std::numeric_limits<int>::max();
    int res = i32_max + 3;

    printf("res %d < i32_max %d is %d\n", res, i32_max, (int)(res < i32_max)); 
// ... is 0
    return (int)(res < i32_max);  // return 1
}
-----------------------------------
static_int_test(): # @static_int_test()
  push rax
  mov edi, offset .L.str
  mov esi, -2147483646
  mov edx, 2147483647
  xor ecx, ecx
  xor eax, eax
  call printf
  mov eax, 1
  pop rcx
  ret
```

Also, clang warns about signed overflow only in the const int case.

-- 
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/20191016/66813406/attachment-0001.html>


More information about the llvm-bugs mailing list