Some fun with -O2
Konstantin Belousov
kostikbel at gmail.com
Thu Jan 14 11:44:48 UTC 2021
On Thu, Jan 14, 2021 at 12:25:23PM +0100, Thierry Thomas wrote:
> Let´s check a small C program, check_mktime.c, fetchable from
> <https://people.freebsd.org/~thierry/check_mktime.c>.
>
> $CC -o check_mktime -O2 check_mktime.c
> ./check_mktime
>
> It is OK with clang90.
> It loops forever with gcc9, gcc10, clang10 and clang11,
> but OK without -O2.
>
> It is part of the configure script of ports/math/oleo, and I guess that
> the same part could be found in other configure scripts.
>
> Note a similar program is part of glibc: see
> <https://code.woboq.org/userspace/glibc/time/tst-mktime2.c.html>.
There is no fun with this stuff.
The time_t type is signed, then the loop
for (time_t_max = 1; 0 < time_t_max; time_t_max *= 2)
continue;
intent is to get signed overflow, which is UB. Modern compilers prefer to
shoot into your foot instead of following common sense.
Workaround is to add -fwrapv compiler switch.
More information about the freebsd-hackers
mailing list