1 << 31 redux
Dimitry Andric
dim at FreeBSD.org
Thu Jan 11 13:37:35 UTC 2018
On 11 Jan 2018, at 13:05, Ed Schouten <ed at nuxi.nl> wrote:
>
> 2018-01-11 13:03 GMT+01:00 Eitan Adler <lists at eitanadler.com>:
>> I'd also like to see if we could find some more general solution, be it a
>> compiler warning, bit set macro, or otherwise.
>
> Does Clang already offer a warning for this? If so, we should consider
> adding it to WARNS=6.
There is a -Wshift-sign-overflow flag, but it isn't enabled by default:
$ clang -Wshift-sign-overflow -c bar.c
bar.c:1:26: warning: signed shift result (0x80000000) sets the sign bit of the shift expression's type ('int') and becomes negative [-Wshift-sign-overflow]
int bar(void) { return 1 << 31; }
~ ^ ~~
I would expect quite a lot of stuff to break if you enable it, though. :)
And of course, there is -fsanitize=undefined, which can catch this kind
of thing at runtime.
-Dimitry
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 223 bytes
Desc: Message signed with OpenPGP
URL: <http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20180111/263cc4a4/attachment.sig>
More information about the freebsd-hackers
mailing list