From: Alan Modra Date: Sat, 7 May 2022 07:40:53 +0000 (+0930) Subject: Fix multiple ubsan warnings in i386-dis.c X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=69464d2267c47e91f9ba77726c745d82b8864c88;p=binutils-gdb.git Fix multiple ubsan warnings in i386-dis.c Commit 39fb369834a3 "opcodes: Make i386-dis.c thread-safe" introduced a number of casts to bfd_signed_vma that cause undefined behaviour with a 32-bit libbfd. Revert those changes. * i386-dis.c (OP_E_memory): Do not cast disp to bfd_signed_vma for negation. (get32, get32s): Don't use bfd_signed_vma here. --- diff --git a/opcodes/i386-dis.c b/opcodes/i386-dis.c index 1e3266329c1..6ef091ea7d7 100644 --- a/opcodes/i386-dis.c +++ b/opcodes/i386-dis.c @@ -11634,7 +11634,7 @@ OP_E_memory (instr_info *ins, int bytemode, int sizeflag) { *ins->obufp++ = '-'; *ins->obufp = '\0'; - disp = - (bfd_signed_vma) disp; + disp = -disp; } if (havedisp) @@ -11739,7 +11739,7 @@ OP_E_memory (instr_info *ins, int bytemode, int sizeflag) { *ins->obufp++ = '-'; *ins->obufp = '\0'; - disp = - (bfd_signed_vma) disp; + disp = -disp; } print_displacement (ins, ins->scratchbuf, disp); @@ -11905,28 +11905,28 @@ get64 (instr_info *ins ATTRIBUTE_UNUSED) static bfd_signed_vma get32 (instr_info *ins) { - bfd_signed_vma x = 0; + bfd_vma x = 0; FETCH_DATA (ins->info, ins->codep + 4); - x = *ins->codep++ & (bfd_signed_vma) 0xff; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 8; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 16; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 24; + x = *ins->codep++ & (bfd_vma) 0xff; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 8; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 16; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 24; return x; } static bfd_signed_vma get32s (instr_info *ins) { - bfd_signed_vma x = 0; + bfd_vma x = 0; FETCH_DATA (ins->info, ins->codep + 4); - x = *ins->codep++ & (bfd_signed_vma) 0xff; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 8; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 16; - x |= (*ins->codep++ & (bfd_signed_vma) 0xff) << 24; + x = *ins->codep++ & (bfd_vma) 0xff; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 8; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 16; + x |= (*ins->codep++ & (bfd_vma) 0xff) << 24; - x = (x ^ ((bfd_signed_vma) 1 << 31)) - ((bfd_signed_vma) 1 << 31); + x = (x ^ ((bfd_vma) 1 << 31)) - ((bfd_vma) 1 << 31); return x; }