[ARM] Fix abs overflow when encoding instructions like strb r1, [r0], #-0
authorFangrui Song <i@maskray.me>
Fri, 18 Aug 2023 20:36:29 +0000 (13:36 -0700)
committerTobias Hieta <tobias@hieta.se>
Mon, 21 Aug 2023 08:05:59 +0000 (10:05 +0200)
Tested by llvm/test/MC/ARM/basic-thumb2-instructions.s.
Caught by newer -fsanitize=signed-integer-overflow (D156821).

(cherry picked from commit d8900f661a6e451be0ca253df3065254008dd93a)

llvm/lib/Target/ARM/MCTargetDesc/ARMMCCodeEmitter.cpp

index dae323e..8c642f6 100644 (file)
@@ -1672,9 +1672,9 @@ getT2AddrModeImm8OffsetOpValue(const MCInst &MI, unsigned OpNum,
 
   // FIXME: Needs fixup support.
   unsigned Value = 0;
-  int32_t tmp = (int32_t)MO1.getImm();
-  if (tmp < 0)
-    tmp = abs(tmp);
+  auto tmp = static_cast<uint32_t>(MO1.getImm());
+  if (static_cast<int32_t>(tmp) < 0)
+    tmp = -tmp;
   else
     Value |= 256; // Set the ADD bit
   Value |= tmp & 255;