Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 30 Apr 2021 00:13:52 +0200
From:      Dimitry Andric <dim@FreeBSD.org>
To:        Hans Petter Selasky <hps@selasky.org>
Cc:        FreeBSD Current <freebsd-current@freebsd.org>, Konstantin Belousov <kib@FreeBSD.org>
Subject:   Re: Undefined compiler behaviour or a compiler bug?
Message-ID:  <84FC71CC-FE92-4327-8536-0A04416432C2@FreeBSD.org>
In-Reply-To: <16b60c22-be04-cbbf-8fc4-a90c4031c529@selasky.org>
References:  <59f81323-74c5-e6a8-a62d-6abdf78902e8@selasky.org> <16b60c22-be04-cbbf-8fc4-a90c4031c529@selasky.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
	charset=us-ascii

On 29 Apr 2021, at 23:11, Hans Petter Selasky <hps@selasky.org> wrote:
>=20
> On 4/29/21 11:06 PM, Hans Petter Selasky wrote:
>> Hi,
>> Can someone please explain what C-compiler flag I'm missing, to make =
this simple C-program terminate?
>> I have a function _abs() which at some point is equal to one, but the =
compiler thinks it is not required to emit the test for the while() at =
all, looking at the assembly code! A printf() clearly verifies that the =
_abs() function has returned one.
>=20
>> clang11 -O2 test.c && ./a.out
>=20
> Looks like -fwrapv fixes the issue.

Indeed, in this case UBSan can help you diagnose the issue. E.g.:

% clang -fsanitize=3Dundefined test.c -o test
% ./test
Y:0x00000001 ABS:0x00000011
Y:0x00000007 ABS:0x00000041
Y:0x00000019 ABS:0x00000281
Y:0x00000027 ABS:0x00000601
Y:0x000000d9 ABS:0x0000b801
Y:0x00000327 ABS:0x0009f001
Y:0x000004d9 ABS:0x00178001
Y:0x00003b27 ABS:0x0dab0001
Y:0x000044d9 ABS:0x12840001
test.c:20:26: runtime error: signed integer overflow: 46341 * 46341 =
cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.c:20:26 in
Y:0x0001bb27 ABS:0xff200001
Y:0x000e44d9 ABS:0x9a400001
Y:0x0011bb27 ABS:0x64000001
Y:0x01ee44d9 ABS:0xc8000001
^C

-Dimitry


--Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F
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.2

iF0EARECAB0WIQR6tGLSzjX8bUI5T82wXqMKLiCWowUCYIsvoAAKCRCwXqMKLiCW
o2ZSAJ98xP+shoXJopMKNkFS3Ydycpz5YgCgm0j8he49bPR6lDyYXM7oGHks4HY=
=lDtn
-----END PGP SIGNATURE-----

--Apple-Mail=_C2BF98D0-CEEF-4A70-9DA6-304A70CFB90F--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?84FC71CC-FE92-4327-8536-0A04416432C2>