qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: qemu-devel@nongnu.org
Cc: patches@linaro.org,
	"Edgar E. Iglesias" <edgar.iglesias@gmail.com>,
	Richard Henderson <rth@twiddle.net>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Eduardo Habkost <ehabkost@redhat.com>,
	Laurent Vivier <laurent@vivier.eu>
Subject: [Qemu-devel] [PATCH for-2.9 3/6] disas/m68k: Avoid unintended sign extension in get_field()
Date: Fri,  3 Mar 2017 15:50:30 +0000	[thread overview]
Message-ID: <1488556233-31246-4-git-send-email-peter.maydell@linaro.org> (raw)
In-Reply-To: <1488556233-31246-1-git-send-email-peter.maydell@linaro.org>

In get_field(), we take an 'unsigned char' value and shift it left,
which implicitly promotes it to 'signed int', before ORing it into an
'unsigned long' type.  If 'unsigned long' is 64 bits then this will
result in a sign extension and the top 32 bits of the result will be
1s.  Add explicit casts to unsigned long before shifting to prevent
this.

(Spotted by Coverity, CID 715697.)

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 disas/m68k.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/disas/m68k.c b/disas/m68k.c
index 073abb9..61b689e 100644
--- a/disas/m68k.c
+++ b/disas/m68k.c
@@ -4685,10 +4685,11 @@ get_field (const unsigned char *data, enum floatformat_byteorders order,
 	/* This is the last byte; zero out the bits which are not part of
 	   this field.  */
 	result |=
-	  (*(data + cur_byte) & ((1 << (len - cur_bitshift)) - 1))
+	  (unsigned long)(*(data + cur_byte)
+			  & ((1 << (len - cur_bitshift)) - 1))
 	    << cur_bitshift;
       else
-	result |= *(data + cur_byte) << cur_bitshift;
+	result |= (unsigned long)*(data + cur_byte) << cur_bitshift;
       cur_bitshift += FLOATFORMAT_CHAR_BIT;
       if (order == floatformat_little)
 	++cur_byte;
-- 
2.7.4

  parent reply	other threads:[~2017-03-03 15:50 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-03 15:50 [Qemu-devel] [PATCH for-2.9 0/6] disas: Fix various coverity nits Peter Maydell
2017-03-03 15:50 ` [Qemu-devel] [PATCH for-2.9 1/6] disas/hppa: Remove dead code Peter Maydell
2017-03-03 19:40   ` Philippe Mathieu-Daudé
2017-03-03 15:50 ` [Qemu-devel] [PATCH for-2.9 2/6] disas/i386: Avoid NULL pointer dereference in error case Peter Maydell
2017-03-03 15:50 ` Peter Maydell [this message]
2017-03-03 18:56   ` [Qemu-devel] [PATCH for-2.9 3/6] disas/m68k: Avoid unintended sign extension in get_field() Laurent Vivier
2017-03-03 15:50 ` [Qemu-devel] [PATCH for-2.9 4/6] disas/microblaze: Avoid unintended sign extension Peter Maydell
2017-03-03 15:58   ` Edgar E. Iglesias
2017-03-03 16:02     ` Peter Maydell
2017-03-03 16:06       ` Edgar E. Iglesias
2017-03-03 15:50 ` [Qemu-devel] [PATCH for-2.9 5/6] disas/cris: " Peter Maydell
2017-03-03 15:58   ` Edgar E. Iglesias
2017-03-03 19:43     ` Philippe Mathieu-Daudé
2017-03-03 15:50 ` [Qemu-devel] [PATCH for-2.9 6/6] disas/arm: " Peter Maydell
2017-03-03 16:11 ` [Qemu-devel] [PATCH for-2.9 0/6] disas: Fix various coverity nits no-reply
2017-03-04  0:00 ` no-reply
2017-03-04 12:44   ` Peter Maydell
2017-03-06  1:03     ` Fam Zheng
2017-03-07 17:06 ` Peter Maydell

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=1488556233-31246-4-git-send-email-peter.maydell@linaro.org \
    --to=peter.maydell@linaro.org \
    --cc=edgar.iglesias@gmail.com \
    --cc=ehabkost@redhat.com \
    --cc=laurent@vivier.eu \
    --cc=patches@linaro.org \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).