void setup() { Serial.begin(9600); } void loop() { int i = 1; while (i > 0) { Serial.println((int16_t)i); i += 10000; delay(1000); } Serial.println(i); }
The above sketch prints ever increasing 32bit numbers when compiled with -flto, and the expected wrapping-to-negative numbers when compiled without -flto.
Is this considered a bug, or does it fall under the "signed integer overflow is undefined, so we can do whatever we want" clause?
(I sort-of leaned to the latter without the (int16_t) cast, but I can't figure out an excuse for the cast not truncating the argument to 16bits.)
(Note that Serial.println() is defined as having a "long" argument)
(Yes, it's an Arduino sketch. Same behavior with 1.8.9 (which has gcc 5.4) and (very recent) 1.8.13 (with gcc 7.3.0))