[llvm-bugs] [Bug 44424] Simple C program produces different output if compiled with -O1 instead of -O0

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Jan 1 06:57:13 PST 2020


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

Roman Lebedev <lebedev.ri at gmail.com> changed:

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

--- Comment #1 from Roman Lebedev <lebedev.ri at gmail.com> ---
(In reply to Dirk Hoffmann from comment #0)
> Test program: main.cpp
> 
> 
> #include <stdint.h>
> #include <stdio.h>
> 
> bool foo(uint32_t x, uint32_t y)
> {
>         uint32_t z = (uint16_t)x * (uint16_t)y;
This line contains UB - the multiplication does not happen in uint16_t,
but the uint16_t values are promoted to int, so this is signed overflow.

https://godbolt.org/z/wPp7Xq

>         return z & 0x80000000;
> }
> 
> int main(int argc, char *argv[])
> {
>         printf("%d\n", foo(0x7FFFF, 0x7FFFF));
> }
> 
> 
> 
> Compiler: 
> 
> Apple clang version 11.0.0 (clang-1100.0.33.8)
> Target: x86_64-apple-darwin18.7.0
> Thread model: posix
> 
> Output:
> 
> > clang -O0 main.cpp; ./a.out 
> 1
> 
> > clang -O1 main.cpp; ./a.out 
> 0

-- 
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/20200101/48959588/attachment.html>


More information about the llvm-bugs mailing list