Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions llvm/include/llvm/Support/LEB128.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
Value = 0;
break;
}
Value += Slice << Shift;
// Once Shift reaches 64 the remaining bytes have already been validated
// above to be pure zero-extension, so they contribute nothing. Performing
// "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
// it.
if (LLVM_LIKELY(Shift < 64))
Value += Slice << Shift;
Shift += 7;
} while (*p++ >= 128);
if (n)
Expand Down Expand Up @@ -187,7 +192,12 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
*n = (unsigned)(p - orig_p);
return 0;
}
Value |= Slice << Shift;
// Once Shift reaches 64 the remaining bytes have already been validated
// above to be pure sign-extension, so they contribute nothing. Performing
// "Slice << Shift" with Shift >= 64 would be undefined behavior, so skip
// it.
if (LLVM_LIKELY(Shift < 64))
Value |= Slice << Shift;
Shift += 7;
++p;
} while (Byte >= 128);
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/Support/LEB128Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ TEST(LEB128Test, DecodeULEB128) {
EXPECT_DECODE_ULEB128_EQ(0x80000000'00000000ul,
"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01");

// Decode overlong ULEB128 whose trailing zero-extension bytes push the shift
// amount to 64 or beyond.
EXPECT_DECODE_ULEB128_EQ(0u, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00");
EXPECT_DECODE_ULEB128_EQ(1u, "\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00");

#undef EXPECT_DECODE_ULEB128_EQ
}

Expand Down Expand Up @@ -216,6 +221,12 @@ TEST(LEB128Test, DecodeSLEB128) {
EXPECT_DECODE_SLEB128_EQ(INT64_MAX,
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00");

// Decode overlong SLEB128 whose trailing sign-extension bytes push the shift
// amount to 64 or beyond.
EXPECT_DECODE_SLEB128_EQ(0L, "\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x00");
EXPECT_DECODE_SLEB128_EQ(-1L, "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");
EXPECT_DECODE_SLEB128_EQ(-2L, "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F");

#undef EXPECT_DECODE_SLEB128_EQ
}

Expand Down