diff mbox series

[for-2.9,6/6] disas/arm: Avoid unintended sign extension

Message ID 1488556233-31246-7-git-send-email-peter.maydell@linaro.org
State Accepted
Commit 43c227f9dd7945bb4a895f841ecdb957bd8a12da
Headers show
Series disas: Fix various coverity nits | expand

Commit Message

Peter Maydell March 3, 2017, 3:50 p.m. UTC
When assembling 'given' from the instruction bytes, C's integer
promotion rules mean we may promote an unsigned char to a signed
integer before shifting it, and then sign extend to a 64-bit long,
which can set the high bits of the long.  The code doesn't in fact
care about the high bits if the long is 64 bits, but this is
surprising, so don't do it.

(Spotted by Coverity, CID 1005404.)

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

---
Arguably 'given' should be uint32_t here rather than 'long', but a
small change to placate Coverity seemed wiser than a wholesale change
of the type of the 'given' variables/arguments through the whole
file, since this is 3rd-party code that's known to work.
---
 disas/arm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

-- 
2.7.4
diff mbox series

Patch

diff --git a/disas/arm.c b/disas/arm.c
index 93c6503..27396dd 100644
--- a/disas/arm.c
+++ b/disas/arm.c
@@ -3901,9 +3901,9 @@  print_insn_arm (bfd_vma pc, struct disassemble_info *info)
 
       status = info->read_memory_func (pc, (bfd_byte *)b, 4, info);
       if (little)
-	given = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
+	given = (b[0]) | (b[1] << 8) | (b[2] << 16) | ((unsigned)b[3] << 24);
       else
-	given = (b[3]) | (b[2] << 8) | (b[1] << 16) | (b[0] << 24);
+	given = (b[3]) | (b[2] << 8) | (b[1] << 16) | ((unsigned)b[0] << 24);
     }
   else
     {