From: Eric Blake <eblake@redhat.com>
To: Peter Maydell <peter.maydell@linaro.org>
Cc: Kevin Wolf <kwolf@redhat.com>,
qemu-devel@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>,
patches@linaro.org
Subject: Re: [Qemu-devel] [PATCH] block/qcow2.h: Avoid "1LL << 63" (shifts into sign bit)
Date: Fri, 23 Aug 2013 10:41:52 -0600 [thread overview]
Message-ID: <521790D0.1060200@redhat.com> (raw)
In-Reply-To: <1377275745-8942-1-git-send-email-peter.maydell@linaro.org>
[-- Attachment #1: Type: text/plain, Size: 1546 bytes --]
On 08/23/2013 10:35 AM, Peter Maydell wrote:
> The expression "1LL << 63" tries to shift the 1 into the sign bit of a
> 'long long', which provokes a clang sanitizer warning:
>
> runtime error: left shift of 1 by 63 places cannot be represented in type 'long long'
Yep, C99 6.5.7p3 states it is undefined to shift a signed number left
across the sign bit:
"The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated
bits are filled with zeros. If E1 has an unsigned type, the value of the
result is E1 × 2E2 , reduced modulo one more than the maximum value
representable in the result type. If E1 has a signed type and
nonnegative value, and E1 × 2E2 is representable in the result type,
then that is the resulting value; otherwise, the behavior is undefined."
Qemu assumes twos-complement arithmetic with sane signed left shifts,
but without a way to tell the compiler our assumptions, it's easier to
just stick with well-defined unsigned shifts.
>
> Use "1ULL << 63" as the definition of QCOW_OFLAG_COPIED instead
> to avoid this. For consistency, we also update the other QCOW_OFLAG
> definitions to use the ULL suffix rather than LL, though only the
> shift by 63 is undefined behaviour.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> block/qcow2.h | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]
next prev parent reply other threads:[~2013-08-23 16:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-08-23 16:35 [Qemu-devel] [PATCH] block/qcow2.h: Avoid "1LL << 63" (shifts into sign bit) Peter Maydell
2013-08-23 16:41 ` Eric Blake [this message]
2013-08-28 9:15 ` Kevin Wolf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=521790D0.1060200@redhat.com \
--to=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=patches@linaro.org \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.