Hi, there!

I am new for using GCC mail list, please forgive me if something is wrong.

I have some issues about how GCC deal with the different optimizations in a
UB program.

For example,

small.cc








*#include <iostream>unsigned long long a;void b(unsigned long long *c, int
h) { *c = h; }int d = 0;int main() {  long e = -5392885090005969454L, f =
-7099246355752164823LL, g = f - d + e;  b(&a, g);  std::cout << a <<
std::endl;}*

Here are the instructions I used and outputs
*$g++-8 --version*
g++-8 (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*$g++-8 -O0 small.cc ; ./a.out*
18446744073174349819
*$g++-8 -O1 small.cc ; ./a.out *
0
*$g++-8 -O0 -fsanitize=undefined small.cc ; ./a.out*
small.cc:6:63: runtime error: signed integer overflow: -5392885090005969454
+ -7099246355752164823 cannot be represented in type 'long int'
18446744073174349819
*$g++-8 -O1 -fsanitize=undefined small.cc ; ./a.out*
small.cc:6:63: runtime error: signed integer overflow: -5392885090005969454
+ -7099246355752164823 cannot be represented in type 'long int'
18446744073174349819

The O2~Os produce the same results with O1. It's so weird I add
"-fsanitize=undefined" with "-O1" and produce a different result compared
to only with  "-O1".

So, my concerns are

* It's a bug in GCC with the above results? Or is it a bug if GCC produces
different results in UB programs with different optimization options?

* How GCC deal with other UB programs, do GCC have any default strategy to
deal with that?

Thank you all, and any suggestions are welcome!

Best,
Haoxin

Reply via email to