[PATCH] D110769: [M68k] Avoid UB in disassembler

Ricky Taylor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 29 13:18:48 PDT 2021


ricky26 created this revision.
ricky26 added reviewers: myhsu, RKSimon.
Herald added a subscriber: hiraditya.
ricky26 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D110769

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


Index: llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
===================================================================
--- llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
+++ llvm/lib/Target/M68k/Disassembler/M68kDisassembler.cpp
@@ -451,7 +451,8 @@
     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,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110769.376022.patch
Type: text/x-patch
Size: 564 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210929/4734c373/attachment.bin>


More information about the llvm-commits mailing list