From owner-freebsd-current@freebsd.org Tue May 2 08:26:21 2017 Return-Path: Delivered-To: freebsd-current@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8B553D5A4B4 for ; Tue, 2 May 2017 08:26:21 +0000 (UTC) (envelope-from nick@van-laarhoven.org) Received: from valery.hibma.org (valery.hibma.org [IPv6:2a02:2308::216:3eff:fe79:3a6c]) by mx1.freebsd.org (Postfix) with ESMTP id 582C9373 for ; Tue, 2 May 2017 08:26:21 +0000 (UTC) (envelope-from nick@van-laarhoven.org) Received: from [IPv6:2001:980:530a:1:f971:3b91:5cc3:9c3e] (unknown [IPv6:2001:980:530a:1:f971:3b91:5cc3:9c3e]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by valery.hibma.org (Postfix) with ESMTPSA id 28CE96E0A17 for ; Tue, 2 May 2017 10:26:18 +0200 (CEST) From: Nick Hibma Content-Type: multipart/signed; boundary="Apple-Mail=_798E884E-DB27-415E-BDBA-9EE6FE0E19FD"; protocol="application/pgp-signature"; micalg=pgp-sha512 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Compiler optimisation bug Message-Id: <3626A28E-9DF5-4775-BAAB-CC6C36D2CD01@van-laarhoven.org> Date: Tue, 2 May 2017 10:26:17 +0200 To: FreeBSD Current Mailing List X-Mailer: Apple Mail (2.3273) X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 02 May 2017 08:26:21 -0000 --Apple-Mail=_798E884E-DB27-415E-BDBA-9EE6FE0E19FD Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii There is a bug in sbin/dhclient.c for large expiry values on 32 bit = platforms where time_t is a uint32_t (bug #218980, = https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D218980). It is = caused by a compiler optimisation that removes an if-statement. The code = below shows the following output, clearly showing that the optimised = case provides a different answer: % cc -O2 main.c -o main.a && ./main.a no opt: 0x7fffffff with opt: 0xfffffffe rephrased: 0x7fffffff The code is as follows: % cat main.c #include #include #define TIME_MAX 2147483647 time_t a =3D TIME_MAX; time_t b =3D TIME_MAX; time_t add_noopt(time_t a, time_t b) __attribute__((optnone)) { a +=3D b; if (a < b) a =3D TIME_MAX; return a; } time_t add_withopt(time_t a, time_t b) { a +=3D b; if (a < b) a =3D TIME_MAX; return a; } time_t add_rephrased(time_t a, time_t b) { if (a < 0 || a > TIME_MAX - b) a =3D TIME_MAX; else a +=3D b; return a; } int main(int argc, char **argv) { printf("no opt: 0x%08x\n", add_noopt(a, b)); printf("with opt: 0x%08x\n", add_withopt(a, b)); printf("rephrased: 0x%08x\n", add_rephrased(a, b)); } Should this be reported to the clang folks? Or is this to be expected = when abusing integer overflows this way? Also: The underlying problem is the fact that time_t is a 32 bit signed = int. Any pointers as to a general method of resolving these time_t = issues? Regards, Nick Hibma nick@van-laarhoven.org -- Open Source: We stand on the shoulders of giants. --Apple-Mail=_798E884E-DB27-415E-BDBA-9EE6FE0E19FD Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=signature.asc Content-Type: application/pgp-signature; name=signature.asc Content-Description: Message signed with OpenPGP -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.30 Comment: GPGTools - https://gpgtools.org iQIcBAEBCgAGBQJZCEKpAAoJEB9yHO7GNHbg0ikP/1Ogp7+cmoBQs938GAuI4n1w 9x+f1MHGa4wx4EuJbMRXpjMvnlENuddvLasSQeKoFK+k5exOyd08zkXSIAn7uUix hqwfCPGkgU/Iyan/UGmG4NaS4CgMA4vvdYj0IxMmqNbHW9nBPKtt3NYlcxe/AXfS dR16JJQHjmizSLlSksPFX1/x2g6X26n6ltKTy5KtQ5o1Cr79gVqd3n5YheNfBbF8 FKHmjEadSdAD9j2NUi3pZenGYME7/o11Ptt5V96gORFRQWDFXIs6C2vBTOFFyVy2 LllSe5p77qUk6wd4ZRXbor4C+JJDGzTfRYzT1m+VKM/xuO9lYA4IK9i27EgsyIoA Mfyf5Hof+T7tAczfJUinxYOvSy0fWcdYZ7vZvVcZ4b4d3FQ/J4yo5vaU1BD9Xmz5 x4MfmGgvzlt/IsbaRv0XhF4KbyRAwEYcSDksEGhh3GGkKLNacP5wLgFFLo+DIdPs frluMKpppoH9wwU8nkVJ1VwWzrANyihg9fU+ydkKHtOVpJPeBruWPidUGtQl8cpH SyH+GU+xBQTipV2Dv++XWagsbdJLeMMScKRM7KYlAcygWch7H5B8TbCCSLfn7Jwz 13EJIuCaTcMf1FZntpGqepcUfBCgJCQ3EupC1GjYmQWcSU07jle/4M4llvvz/KKN 8poY2TjRPQjcWDVrp5eD =K/a9 -----END PGP SIGNATURE----- --Apple-Mail=_798E884E-DB27-415E-BDBA-9EE6FE0E19FD--