[ARM] Fix abs overflow when encoding instructions like strb r1, [r0], #-0

Tested by llvm/test/MC/ARM/basic-thumb2-instructions.s.
Caught by newer -fsanitize=signed-integer-overflow (D156821).

(cherry picked from commit d8900f661a6e451be0ca253df3065254008dd93a)
This commit is contained in:
Fangrui Song 2023-08-18 13:36:29 -07:00 committed by Tobias Hieta
parent 3444abf2f3
commit 0c756c5906

View 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;