https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106025
Bug ID: 106025
Summary: Incorrect optimization at -O2 leads to infinite test
execution time
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: vsevolod.livinskiy at gmail dot com
Target Milestone: ---
The GCC incorrectly optimizes the code, which leads to infinite (or very large)
execution time.
Unfortunately, I was not able to merge the reproducer into a single file.
This error was discovered by a fuzzer. If you think that it is not valuable or
relevant, feel free to close the bug report.
Error:
>$ g++ -O1 func.cpp driver.cpp && time ./a.out
4294965301
real 0m0.001s
user 0m0.001s
sys 0m0.000s
>$ g++ -O2 func.cpp driver.cpp && time ./a.out
^C
real 160m11.604s
user 160m10.790s
sys 0m0.000s
Reproducer:
//driver.cpp
#include <stdio.h>
short var_1 = (short)-1995;
unsigned short var_8 = (unsigned short)22379;
unsigned long long int var_154;
unsigned char var_355 = 3;
short var_371 = 27541;
short var_385 = 29482;
short var_401;
void test(short var_1, unsigned short var_8, unsigned char *var_355, short
*var_371 , short var_385);
int main() {
test(var_1, var_8, &var_355, &var_371, var_385);
printf("%llu\n", var_154);
}
//func.cpp
extern int var_154;
extern short var_401;
const unsigned short &min(const unsigned short &b, unsigned short &c) {
return b < c ? b : c;
}
const unsigned short &max(const unsigned short &b, const unsigned short &c) {
return b > c ? b : c;
}
void test(short var_1, unsigned short var_8,
unsigned char *var_355, short *var_371, short var_385) {
var_154 = var_1;
for (short i = 0; i < (short)max(var_1, min(*var_355, var_8)) + 7; i =
var_385)
var_401 = *var_371 ? *var_371 : 5;
}
gcc version 13.0.0 20220618 (479b6f449ee999501ad6eff0b7db8d0cd5b2d28d)