ubsan: score: left shift of negative value

Message ID 20200113014829.GM4433@bubble.grove.modra.org
State New
Headers show
Series
  • ubsan: score: left shift of negative value
Related show

Commit Message

Alan Modra Jan. 13, 2020, 1:48 a.m.
* score-dis.c (print_insn_score48): Use unsigned variables for
	unsigned values.  Don't left shift negative values.
	(print_insn_score32): Likewise.
	* score7-dis.c (print_insn_score32, print_insn_score16): Likewise.


-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/opcodes/score-dis.c b/opcodes/score-dis.c
index 6b98a3359f..6f37dfdc4f 100644
--- a/opcodes/score-dis.c
+++ b/opcodes/score-dis.c
@@ -565,7 +565,7 @@  print_insn_score48 (struct disassemble_info *info, bfd_vma given)
                               {
                               case 'r':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
@@ -575,7 +575,7 @@  print_insn_score48 (struct disassemble_info *info, bfd_vma given)
                                 break;
                               case 'd':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
@@ -600,14 +600,14 @@  print_insn_score48 (struct disassemble_info *info, bfd_vma given)
                                       || ((given & insn->mask) == 0x0c00000b)   /* stc1  */
                                       || ((given & insn->mask) == 0x0c000013)   /* stc2  */
                                       || ((given & insn->mask) == 0x0c00001b))  /* stc3  */
-                                    reg <<= 2;
+                                    reg *= 4;
 
                                   func (stream, "%ld", reg);
                                 }
                                 break;
                               case 'x':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
@@ -617,7 +617,7 @@  print_insn_score48 (struct disassemble_info *info, bfd_vma given)
                                 break;
                                 case 'w':
                                 {
-                                    long reg;
+                                    unsigned long reg;
                                     reg = given >> bitstart;
                                     reg &= (2u << (bitend - bitstart)) - 1;
                                     reg <<= 2;
@@ -731,7 +731,7 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                            }
                            else
                            {
-                               long reg;
+                               unsigned long reg;
                                int bitstart = 10;
                                int bitend = 14;
                                reg = given >> bitstart;
@@ -784,7 +784,7 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                               {
                               case 'r':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
@@ -794,7 +794,7 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                                 break;
                               case 'd':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
@@ -817,14 +817,14 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                                       || ((given & insn->mask) == 0x0c00000b)   /* stc1  */
                                       || ((given & insn->mask) == 0x0c000013)   /* stc2  */
                                       || ((given & insn->mask) == 0x0c00001b))  /* stc3  */
-                                    reg <<= 2;
+                                    reg *= 4;
 
                                   func (stream, "%ld", reg);
                                 }
                                 break;
                               case 'x':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
                                   reg &= (2u << (bitend - bitstart)) - 1;
diff --git a/opcodes/score7-dis.c b/opcodes/score7-dis.c
index b1b7734180..c9b235b7b4 100644
--- a/opcodes/score7-dis.c
+++ b/opcodes/score7-dis.c
@@ -613,20 +613,20 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                               {
                               case 'r':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
-                                  reg &= (2 << (bitend - bitstart)) - 1;
+                                  reg &= (2u << (bitend - bitstart)) - 1;
 
                                   func (stream, "%s", score_regnames[reg]);
                                 }
                                 break;
                               case 'd':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
-                                  reg &= (2 << (bitend - bitstart)) - 1;
+                                  reg &= (2u << (bitend - bitstart)) - 1;
 
                                   func (stream, "%ld", reg);
                                 }
@@ -636,9 +636,9 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                                   long reg;
 
                                   reg = given >> bitstart;
-                                  reg &= (2 << (bitend - bitstart)) - 1;
-                                  reg = ((reg ^ (1 << (bitend - bitstart))) -
-                                        (1 << (bitend - bitstart)));
+                                  reg &= (2u << (bitend - bitstart)) - 1;
+                                  reg = ((reg ^ (1 << (bitend - bitstart)))
+					 - (1 << (bitend - bitstart)));
 
                                   if (((given & insn->mask) == 0x0c00000a)      /* ldc1  */
                                       || ((given & insn->mask) == 0x0c000012)   /* ldc2  */
@@ -646,17 +646,17 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                                       || ((given & insn->mask) == 0x0c00000b)   /* stc1  */
                                       || ((given & insn->mask) == 0x0c000013)   /* stc2  */
                                       || ((given & insn->mask) == 0x0c00001b))  /* stc3  */
-                                    reg <<= 2;
+                                    reg *= 4;
 
                                   func (stream, "%ld", reg);
                                 }
                                 break;
                               case 'x':
                                 {
-                                  long reg;
+                                  unsigned long reg;
 
                                   reg = given >> bitstart;
-                                  reg &= (2 << (bitend - bitstart)) - 1;
+                                  reg &= (2u << (bitend - bitstart)) - 1;
 
                                   func (stream, "%lx", reg);
                                 }
@@ -667,12 +667,12 @@  print_insn_score32 (bfd_vma pc, struct disassemble_info *info, long given)
                             break;
                           case '`':
                             c++;
-                            if ((given & (1 << bitstart)) == 0)
+                            if ((given & (1u << bitstart)) == 0)
                               func (stream, "%c", *c);
                             break;
                           case '\'':
                             c++;
-                            if ((given & (1 << bitstart)) != 0)
+                            if ((given & (1u << bitstart)) != 0)
                               func (stream, "%c", *c);
                             break;
                           default:
@@ -789,7 +789,7 @@  print_insn_score16 (bfd_vma pc, struct disassemble_info *info, long given)
                               if (!bitend)
                                 abort ();
                               reg = given >> bitstart;
-                              reg &= (2 << (bitend - bitstart)) - 1;
+                              reg &= (2u << (bitend - bitstart)) - 1;
                               switch (*c)
                                 {
                                 case 'R':
@@ -835,7 +835,7 @@  print_insn_score16 (bfd_vma pc, struct disassemble_info *info, long given)
 
                           case '\'':
                             c++;
-                            if ((given & (1 << bitstart)) != 0)
+                            if ((given & (1u << bitstart)) != 0)
                               func (stream, "%c", *c);
                             break;
                           default: