Skip to content

Commit fb798d3

Browse files
catenacyberaquynh
authored andcommittedJun 2, 2018
Undefined shifts (capstone-engine#1154)
* Fix undefined shifts uint8 gets promoted to signed integer in ARM, MIPS, Sparc in AArch64, PPC and Xcore * fix undefined shift in powerpc * Fix undefined shift in Mips use mulitply instead
1 parent e8cb987 commit fb798d3

10 files changed

+18
-18
lines changed
 

‎LEB128.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n)
2727
uint64_t Value = 0;
2828
unsigned Shift = 0;
2929
do {
30-
Value += (*p & 0x7f) << Shift;
30+
Value += (uint64_t)(*p & 0x7f) << Shift;
3131
Shift += 7;
3232
} while (*p++ >= 128);
3333
if (n)

‎arch/AArch64/AArch64AddressingModes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static inline float AArch64_AM_getFPImmFloat(unsigned Imm)
198198
// where B = NOT(b);
199199

200200
FPUnion.I = 0;
201-
FPUnion.I |= Sign << 31;
201+
FPUnion.I |= (uint32_t) Sign << 31;
202202
FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
203203
FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
204204
FPUnion.I |= (Exp & 0x3) << 23;

‎arch/AArch64/AArch64Disassembler.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ static DecodeStatus _getInstruction(cs_struct *ud, MCInst *MI,
240240

241241
if (ud->big_endian)
242242
insn = (code[3] << 0) | (code[2] << 8) |
243-
(code[1] << 16) | (code[0] << 24);
243+
(code[1] << 16) | ((uint32_t) code[0] << 24);
244244
else
245-
insn = (code[3] << 24) | (code[2] << 16) |
245+
insn = ((uint32_t) code[3] << 24) | (code[2] << 16) |
246246
(code[1] << 8) | (code[0] << 0);
247247

248248
// Calling the auto-generated decoder function.

‎arch/ARM/ARMAddressingModes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ static inline float getFPImmFloat(unsigned Imm)
658658
// where B = NOT(b);
659659

660660
FPUnion.I = 0;
661-
FPUnion.I |= Sign << 31;
661+
FPUnion.I |= (uint32_t) Sign << 31;
662662
FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
663663
FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
664664
FPUnion.I |= (Exp & 0x3) << 23;

‎arch/ARM/ARMDisassembler.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,9 @@ static DecodeStatus _ARM_getInstruction(cs_struct *ud, MCInst *MI, const uint8_t
486486
insn = (code[3] << 0) |
487487
(code[2] << 8) |
488488
(code[1] << 16) |
489-
(code[0] << 24);
489+
((uint32_t) code[0] << 24);
490490
else
491-
insn = (code[3] << 24) |
491+
insn = ((uint32_t) code[3] << 24) |
492492
(code[2] << 16) |
493493
(code[1] << 8) |
494494
(code[0] << 0);
@@ -780,11 +780,11 @@ static DecodeStatus _Thumb_getInstruction(cs_struct *ud, MCInst *MI, const uint8
780780
insn32 = (code[3] << 0) |
781781
(code[2] << 8) |
782782
(code[1] << 16) |
783-
(code[0] << 24);
783+
((uint32_t) code[0] << 24);
784784
else
785785
insn32 = (code[3] << 8) |
786786
(code[2] << 0) |
787-
(code[1] << 24) |
787+
((uint32_t) code[1] << 24) |
788788
(code[0] << 16);
789789

790790
MCInst_clear(MI);

‎arch/Mips/MipsDisassembler.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -391,14 +391,14 @@ static void readInstruction32(unsigned char *code, uint32_t *insn, bool isBigEnd
391391
if (isBigEndian) {
392392
// Encoded as a big-endian 32-bit word in the stream.
393393
*insn =
394-
(code[3] << 0) | (code[2] << 8) | (code[1] << 16) | (code[0] << 24);
394+
(code[3] << 0) | (code[2] << 8) | (code[1] << 16) | ((uint32_t) code[0] << 24);
395395
} else {
396396
if (isMicroMips) {
397397
*insn = (code[2] << 0) | (code[3] << 8) | (code[0] << 16) |
398-
(code[1] << 24);
398+
((uint32_t) code[1] << 24);
399399
} else {
400400
*insn = (code[0] << 0) | (code[1] << 8) | (code[2] << 16) |
401-
(code[3] << 24);
401+
((uint32_t) code[3] << 24);
402402
}
403403
}
404404
}
@@ -1786,7 +1786,7 @@ static DecodeStatus DecodeMovePRegPair(MCInst *Inst, unsigned Insn,
17861786
static DecodeStatus DecodeSimm23Lsl2(MCInst *Inst, unsigned Insn,
17871787
uint64_t Address, MCRegisterInfo *Decoder)
17881788
{
1789-
MCOperand_CreateImm0(Inst, SignExtend32(Insn, 23) << 2);
1789+
MCOperand_CreateImm0(Inst, SignExtend32(Insn, 23) * 4);
17901790
return MCDisassembler_Success;
17911791
}
17921792

‎arch/PowerPC/PPCDisassembler.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ static DecodeStatus getInstruction(MCInst *MI,
364364

365365
// The instruction is big-endian encoded.
366366
if (MI->csh->mode & CS_MODE_BIG_ENDIAN)
367-
insn = (code[0] << 24) | (code[1] << 16) |
367+
insn = ((uint32_t) code[0] << 24) | (code[1] << 16) |
368368
(code[2] << 8) | (code[3] << 0);
369369
else
370-
insn = (code[3] << 24) | (code[2] << 16) |
370+
insn = ((uint32_t) code[3] << 24) | (code[2] << 16) |
371371
(code[1] << 8) | (code[0] << 0);
372372

373373
if (MI->flat_insn->detail) {

‎arch/PowerPC/PPCInstPrinter.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ static void printAbsBranchOperand(MCInst *MI, unsigned OpNo, SStream *O)
567567
return;
568568
}
569569

570-
imm = MCOperand_getImm(MCInst_getOperand(MI, OpNo)) << 2;
570+
imm = MCOperand_getImm(MCInst_getOperand(MI, OpNo)) * 4;
571571

572572
if (!PPC_abs_branch(MI->csh, MCInst_getOpcode(MI))) {
573573
imm = MI->address + imm;

‎arch/Sparc/SparcDisassembler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static DecodeStatus readInstruction32(const uint8_t *code, size_t len, uint32_t
212212
*Insn = (code[3] << 0) |
213213
(code[2] << 8) |
214214
(code[1] << 16) |
215-
(code[0] << 24);
215+
((uint32_t) code[0] << 24);
216216

217217
return MCDisassembler_Success;
218218
}

‎arch/XCore/XCoreDisassembler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ static bool readInstruction32(const uint8_t *code, size_t code_len, uint32_t *in
5050
return false;
5151

5252
// Encoded as a little-endian 32-bit word in the stream.
53-
*insn = (code[0] << 0) | (code[1] << 8) | (code[2] << 16) | (code[3] << 24);
53+
*insn = (code[0] << 0) | (code[1] << 8) | (code[2] << 16) | ((uint32_t) code[3] << 24);
5454
return true;
5555
}
5656

0 commit comments

Comments
 (0)
Please sign in to comment.