Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Jan 2021 13:44:34 +0200
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Thierry Thomas <thierry@freebsd.org>
Cc:        FreeBSD Hackerz <hackers@freebsd.org>
Subject:   Re: Some fun with -O2
Message-ID:  <YAAuouAngRyT7Dw0@kib.kiev.ua>
In-Reply-To: <YAAqI08bgBtfpssP@graf.pompo.net>
References:  <YAAqI08bgBtfpssP@graf.pompo.net>

next in thread | previous in thread | raw e-mail | index | archive | help
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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?YAAuouAngRyT7Dw0>