Created attachment 1471601 [details] test case Description of problem: Incorrect compilation of input code. Consider the paste here (also attached): https://paste.fedoraproject.org/paste/H5sSuizGND6RZBp0G4Amfg/raw Under -O2, this code is compiled incorrectly. In particular, the function sets errno = ERANGE, because (n != INT64_MIN) -- on line 82, even though the contents of memory show that n == INT64_MIN, that (n*p < 0), and s[-1] == '8'. Hence the if test should fail (as !(true && true) == false) and errno should be zero. See: $ gcc -O2 tc.c -o tc && ./tc errno: 34 out: -9223372036854775808 $ gcc -O1 tc.c -o tc && ./tc errno: 0 out: -9223372036854775808 This also happens under -O3, and does not happen under -Og or -O0. Clang compiles this code correctly. Version-Release number of selected component (if applicable): gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5) 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. How reproducible: Completely, see test case. Steps to Reproduce: 1. Download the paste, inspect the code. 2. Compile with -O2 or higher, run the resulting executable. Actual results: strto_int64_dec exits with status 34. Expected results: strto_int64_dec exists with status 0. Additional info: Have not tested on non-Intel architectures.
$ gcc -fsanitize=undefined -O3 t.c $ ./a.out t.c:70:19: runtime error: signed integer overflow: 9223372036854775800 + 8 cannot be represented in type 'long int' t.c:75:11: runtime error: signed integer overflow: -9223372036854775808 * -1 cannot be represented in type 'long int' t.c:82:22: runtime error: signed integer overflow: -9223372036854775808 * -1 cannot be represented in type 'long int' errno: 0 out: -9223372036854775808 $ gcc -fsanitize=undefined -O3 t.c -fwrapv $ ./a.out errno: 0 out: -9223372036854775808 This matches the source code, so there is no compiler bug.
Thanks Florian! I'll remember -fsanitize=undefined for next time :)