From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44697) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z0tyC-0002xb-Sf for qemu-devel@nongnu.org; Fri, 05 Jun 2015 11:55:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z0tyB-0002ew-Og for qemu-devel@nongnu.org; Fri, 05 Jun 2015 11:55:44 -0400 Message-ID: <5571C679.2010202@redhat.com> Date: Fri, 05 Jun 2015 17:55:37 +0200 From: Paolo Bonzini MIME-Version: 1.0 References: <1433367941-119488-1-git-send-email-agraf@suse.de> <5571B3BE.60003@redhat.com> <5571BA13.3040507@redhat.com> <5571BE3D.9050002@redhat.com> In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: [Qemu-devel] undefined behavior of signed left shifts (was Re: [PULL 00/40] ppc patch queue 2015-06-03) List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Peter Maydell Cc: Michael Roth , gcc@gcc.gnu.org, "qemu-ppc@nongnu.org" , Alexander Graf , QEMU Developers 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 only implementation-defined. > 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. Paolo