[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#945991: gcc-8 makes wrong integer comparison when using optimization flag



Package: gcc-8
Version: 8.3.0-6
Severity: important
File: /usr/bin/gcc

Dear Maintainer,

I have encountered a bug on the simple code below:


#include <stdio.h>

int fits(int x, int n)
{
int TMinn = -(1 << (n-1));
int TMaxn = (1 << (n-1)) - 1;
return x >= TMinn && x <= TMaxn;
}

int main(void)
{
int cnt;
int x = 1<<31;

for(cnt = 1; cnt<=32; cnt++)
{
if (fits(x,cnt))
{
printf(“%d : ok\n”, cnt);
}
else
{
printf(“%d : ko\n”, cnt);
}
}
return 0;
}

When compiled without optimization, the code produces a correct output (comparison is always false except for n=32):


sfaucou@sfaucou-laptop:~/Temp$ gcc bug.c
sfaucou@sfaucou-laptop:~/Temp$ ./a.out
1 : ko
2 : ko
3 : ko
4 : ko
5 : ko
6 : ko
7 : ko
8 : ko
9 : ko
10 : ko
11 : ko
12 : ko
13 : ko
14 : ko
15 : ko
16 : ko
17 : ko
18 : ko
19 : ko
20 : ko
21 : ko
22 : ko
23 : ko
24 : ko
25 : ko
26 : ko
27 : ko
28 : ko
29 : ko
30 : ko
31 : ko
32 : ok

When compiling with optimization, it produces a wrong output (comparison is evaluated to false for n=32):


sfaucou@sfaucou-laptop:~/Temp$ gcc -O bug.c
sfaucou@sfaucou-laptop:~/Temp$ ./a.out
1 : ko
2 : ko
3 : ko
4 : ko
5 : ko
6 : ko
7 : ko
8 : ko
9 : ko
10 : ko
11 : ko
12 : ko
13 : ko
14 : ko
15 : ko
16 : ko
17 : ko
18 : ko
19 : ko
20 : ko
21 : ko
22 : ko
23 : ko
24 : ko
25 : ko
26 : ko
27 : ko
28 : ko
29 : ko
30 : ko
31 : ko
32 : ko

>From what I have seen from the generated code, the optimized version computes the result at compile-time and simply prints the message “ko”.

I have tested the code with various optimization options (-O, -O1, -O2, -O3), for ia32 and x86-64 ISA. I did also reproduce the bug on machine running Ubuntu (same version of gcc).

Syste information as produced by reportbug are given below.

Let me know if you need further information or if I can help in some other way.

Best regards,

Sébastien.

– System Information:
Debian Release: 10.2
APT prefers stable-updates
APT policy: (500, ’stable-updates’), (500, ’stable’)
Architecture: amd64 (x8664)
Foreign Architectures: i386

Kernel: Linux 4.19.0-6-amd64 (SMP w/4 CPU cores)
Kernel taint flags: TAINTOOTMODULE, TAINTUNSIGNEDMODULE
Locale: LANG=frFR.UTF-8, LCCTYPE=frFR.UTF-8 (charmap=UTF-8), LANGUAGE= (charmap=UTF-8)
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages gcc-8 depends on:
ii binutils 2.31.1-16
ii cpp-8 8.3.0-6
ii gcc-8-base 8.3.0-6
ii libc6 2.28-10
ii libcc1-0 8.3.0-6
ii libgcc-8-dev 8.3.0-6
ii libgcc1 1:8.3.0-6
ii libgmp10 2:6.1.2+dfsg-4
ii libisl19 0.20-2
ii libmpc3 1.1.0-1
ii libmpfr6 4.0.2-1
ii libstdc++6 8.3.0-6
ii zlib1g 1:1.2.11.dfsg-1

Versions of packages gcc-8 recommends:
ii libc6-dev 2.28-10

Versions of packages gcc-8 suggests:
pn gcc-8-doc <none>
pn gcc-8-locales <none>
ii gcc-8-multilib 8.3.0-6
pn libasan5-dbg <none>
pn libatomic1-dbg <none>
pn libgcc1-dbg <none>
pn libgomp1-dbg <none>
pn libitm1-dbg <none>
pn liblsan0-dbg <none>
pn libmpx2-dbg <none>
pn libquadmath0-dbg <none>
pn libtsan0-dbg <none>
pn libubsan1-dbg <none>

– no debconf information


Reply to: