Skip to contentSkip to conversation

disas/sparc: Fix integer overflow in compare_opcodes()

PATCH In review [email protected] 2 participants 2 messages Feb 18, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 disas/sparc: Fix integer overflow in compare_opcodes()

From: Denis Sergeev <[email protected]>

Fix an integer overflow issue caused by a left shift operation (1 << i) on an int literal. For i >= 31, this could lead to undefined behavior by exceeding the 32-bit range.

To prevent this, explicitly cast the literal to an unsigned long int (1UL << i), ensuring the shift operation is performed safely.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2618

---
 disas/sparc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
disas/sparc.c +2 -2
index 5689533ce1..92b9ac754f 100644
@@ -2515,7 +2515,7 @@ compare_opcodes (const void * a, const void * b)
      another, it is important to order the opcodes in the right order.  */
   for (i = 0; i < 32; ++i)
     {
-      unsigned long int x = 1 << i;
+      unsigned long int x = 1UL << i;
       int x0 = (match0 & x) != 0;
       int x1 = (match1 & x) != 0;
 
@@ -2525,7 +2525,7 @@ compare_opcodes (const void * a, const void * b)
 
   for (i = 0; i < 32; ++i)
     {
-      unsigned long int x = 1 << i;
+      unsigned long int x = 1UL << i;
       int x0 = (lose0 & x) != 0;
       int x1 = (lose1 & x) != 0;
 
Signature · 1 line
2.42.4
#1 lore raw
Quoted 6 lines · On Tue, 18 Feb 2025 at 09:00, <[email protected]> wrote:

From: Denis Sergeev <[email protected]>

Fix an integer overflow issue caused by a left shift operation (1 << i) on an int literal. For i >= 31, this could lead to undefined behavior by exceeding the 32-bit range.

The only case here where i >= 31 is exactly i == 31. QEMU compiles with -fwrapv, so in our dialect of C a shift left of signed integer 1 into the sign bit is *not* undefined behaviour.

thanks
-- PMM