Skip to contentSkip to conversation

disas: sparc: Fix integer overflow in compare_opcodes() for loop

PATCH Superseded Christian Barry 4 participants 4 messages May 27 – May 28

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

From: Christian Barry <[email protected]>

Replaced left-shift of a literal 1 by i inside of two for loops in compare_opcodes() with a call to BIT(i). This makes it so 1 is interpreted as an unsigned long int, preventing overflows.

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

---
 disas/sparc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
disas/sparc.c +3 -2
index 5689533ce1..6fc057e3a2 100644
@@ -27,6 +27,7 @@
    see <http://www.gnu.org/licenses/>.  */
 
 #include "qemu/osdep.h"
+#include "qemu/bitops.h"
 #include "disas/dis-asm.h"
 
 /* The SPARC opcode table (and other related data) is defined in
@@ -2515,7 +2516,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 = BIT(i);
       int x0 = (match0 & x) != 0;
       int x1 = (match1 & x) != 0;
 
@@ -2525,7 +2526,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 = BIT(i);
       int x0 = (lose0 & x) != 0;
       int x1 = (lose1 & x) != 0;
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Le 27/05/2026 à 19:57, Christian Barry a écrit :

Quoted 9 lines

From: Christian Barry <[email protected]>

Replaced left-shift of a literal 1 by i inside of two for loops in compare_opcodes() with a call to BIT(i). This makes it so 1 is interpreted as an unsigned long int, preventing overflows.

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

Quoted 34 lines
---
  disas/sparc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
disas/sparc.c +3 -2
index 5689533ce1..6fc057e3a2 100644
@@ -27,6 +27,7 @@
     see <http://www.gnu.org/licenses/>.  */
 
  #include "qemu/osdep.h"
+#include "qemu/bitops.h"
  #include "disas/dis-asm.h"
 
  /* The SPARC opcode table (and other related data) is defined in
@@ -2515,7 +2516,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 = BIT(i);
        int x0 = (match0 & x) != 0;
        int x1 = (match1 & x) != 0;
 
@@ -2525,7 +2526,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 = BIT(i);
        int x0 = (lose0 & x) != 0;
        int x1 = (lose1 & x) != 0;
 
#2 lore raw
In reply to Christian Barry
Quoted 27 lines · Christian Barry

From: Christian Barry <[email protected]>

Replaced left-shift of a literal 1 by i inside of two for loops in compare_opcodes() with a call to BIT(i). This makes it so 1 is interpreted as an unsigned long int, preventing overflows.

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

---
 disas/sparc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
disas/sparc.c +1 -0
index 5689533ce1..6fc057e3a2 100644
@@ -27,6 +27,7 @@
    see <http://www.gnu.org/licenses/>.  */
 
 #include "qemu/osdep.h"
+#include "qemu/bitops.h"
 #include "disas/dis-asm.h"
 
 /* The SPARC opcode table (and other related data) is defined in

disas/sparc.c compare_opcodes (const void * a, const void * b) · line 2517

Quoted 2 lines
disas/sparc.c +0 -0
@@ -2515,7 +2516,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)

The loop boundary is 32, so i is at most 1.

disas/sparc.c compare_opcodes (const void * a, const void * b) · line 2519

Quoted 3 lines
disas/sparc.c +1 -1
     {
-      unsigned long int x = 1 << i;
+      unsigned long int x = BIT(i);

The loop boundary is 32, so i is at most 31. QEMU uses the -fwrapv compiler option, so "1 << 31" is well defined, even though it shifts into the sign bit. It's worth noting in the commit message that we are not actually fixing any bug here, we are merely placating somebody's static analyzer.

disas/sparc.c compare_opcodes (const void * a, const void * b) · line 2531

Quoted 11 lines
disas/sparc.c +1 -1
       int x0 = (match0 & x) != 0;
       int x1 = (match1 & x) != 0;
 
@@ -2525,7 +2526,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 = BIT(i);
       int x0 = (lose0 & x) != 0;
       int x1 = (lose1 & x) != 0;

If we're going to bother to change this not-a-bug, it would be better to do it the way that upstream binutils did it, as noted in the gitlab issue: using "1ul".

-- PMM

#3 lore raw
Quoted 12 lines · Peter Maydell

disas/sparc.c compare_opcodes (const void * a, const void * b) · line 2531

Quoted 8 lines
disas/sparc.c +1 -1
@@ -2525,7 +2526,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 = BIT(i);
        int x0 = (lose0 & x) != 0;
        int x1 = (lose1 & x) != 0;

If we're going to bother to change this not-a-bug, it would be better to do it the way that upstream binutils did it, as noted in the gitlab issue: using "1ul".

I would go further and use uint32_t not unsigned long at all. If we're going to change anything.

r~