[M68k] Avoid UB in disassembler
authorRicky Taylor <rickytaylor26@gmail.com>
Wed, 29 Sep 2021 20:05:54 +0000 (21:05 +0100)
committerRicky Taylor <rickytaylor26@gmail.com>
Wed, 29 Sep 2021 21:07:14 +0000 (22:07 +0100)
When reading 32 bits a 32-bit shift would be executed.

This is undefined behaviour, but in this case we can just replace the
entire scratch value to avoid it.

Differential Revision: https://reviews.llvm.org/D110769

llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp

index a8453c8..1e397f0 100644 (file)
@@ -451,7 +451,8 @@ void M68kDisassembler::decodeImm(MCInst &Instr, unsigned Bead,
     llvm_unreachable("invalid imm");
   }
 
-  Scratch = (Scratch << NumToRead) | Reader.readBits(NumToRead);
+  Scratch = (NumToRead < 32) ? (Scratch << NumToRead) : 0;
+  Scratch |= Reader.readBits(NumToRead);
 }
 
 DecodeStatus M68kDisassembler::getInstruction(MCInst &Instr, uint64_t &Size,