Skip to content

Commit d23f5a5

Browse files
committed
[LEB128] Don't handle edge cases in every loop iteration
Previously the overflow check was done for every byte even though it is only needed for the case where Shift == 63. (cherry picked from commit 963b186)
1 parent 4e58c0d commit d23f5a5

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

llvm/include/llvm/Support/LEB128.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
142142
break;
143143
}
144144
uint64_t Slice = *p & 0x7f;
145-
if ((Shift >= 64 && Slice != 0) || Slice << Shift >> Shift != Slice) {
145+
if (Shift >= 63 && ((Shift == 63 && (Slice << Shift >> Shift) != Slice) ||
146+
(Shift > 63 && Slice != 0))) {
146147
if (error)
147148
*error = "uleb128 too big for uint64";
148149
Value = 0;
@@ -177,8 +178,8 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
177178
}
178179
Byte = *p;
179180
uint64_t Slice = Byte & 0x7f;
180-
if ((Shift >= 64 && Slice != (Value < 0 ? 0x7f : 0x00)) ||
181-
(Shift == 63 && Slice != 0 && Slice != 0x7f)) {
181+
if ((Shift >= 63) && ((Shift == 63 && Slice != 0 && Slice != 0x7f) ||
182+
(Shift > 63 && Slice != (Value < 0 ? 0x7f : 0x00)))) {
182183
if (error)
183184
*error = "sleb128 too big for int64";
184185
if (n)

0 commit comments

Comments
 (0)