This is the mail archive of the
mailing list for the GCC project.
undefined behavior of signed left shifts (was Re: [PULL 00/40] ppc patch queue 2015-06-03)
- From: Paolo Bonzini <pbonzini at redhat dot com>
- To: Peter Maydell <peter dot maydell at linaro dot org>
- Cc: Alexander Graf <agraf at suse dot de>, "qemu-ppc at nongnu dot org" <qemu-ppc at nongnu dot org>, QEMU Developers <qemu-devel at nongnu dot org>, Michael Roth <mdroth at linux dot vnet dot ibm dot com>, gcc at gcc dot gnu dot org
- Date: Fri, 05 Jun 2015 17:55:37 +0200
- Subject: undefined behavior of signed left shifts (was Re: [PULL 00/40] ppc patch queue 2015-06-03)
- Authentication-results: sourceware.org; auth=none
- References: <1433367941-119488-1-git-send-email-agraf at suse dot de> <CAFEAcA_qVShs0+FwRGVXE=u5g1Bx9Z6knoG71rnE9mcKSEwyEg at mail dot gmail dot com> <5571B3BE dot 60003 at redhat dot com> <CAFEAcA9ZrQPf1phr5ffuzXMcVGFuGDQWqK=YmGzY2faC36p49A at mail dot gmail dot com> <5571BA13 dot 3040507 at redhat dot com> <CAFEAcA-BLmhegzqVHUH88PfHxAnBq8QYBW8UNUp858+08Db9zA at mail dot gmail dot com> <5571BE3D dot 9050002 at redhat dot com> <CAFEAcA9wASC_-dtbup=jX36NPqVw50GrhP8R-asApnFFSv5jgw at mail dot gmail dot com>
On 05/06/2015 17:45, Peter Maydell wrote:
>>> ...but things like "(1U << 31)" are entirely valid.
>> They're only valid until someone does a ~ on them. I think it's
>> reasonable to forbid them in our coding standards, if we want to fix
>> ubsan's warning of (1 << 31).
>> I don't think it's reasonable for compiler writers to exploit the
>> undefinedness of (1 << 31) anyway, and if it were possible to shut up
>> ubsan about this particular kind of undefined behavior, I would prefer it.
> I don't think it's reasonable for compiler writers to exploit
> undefined behaviour either, but historically they absolutely
> have done.
Most cases of undefined behavior are rooted in "you should never do that
anyway". This is not the case for bitwise operations, since they are
not mathematical concepts and the representation of integers as bits is
> Absent a guarantee from gcc that it will never do
> so, I think we should avoid any UB in our code.
The GCC manual says "GCC does not use the latitude given in C99 and C11
only to treat certain aspects of signed '<<' as undefined, but this is
subject to change". It would certainly be nice if they removed the
"this is subject to change" part.