1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[llvm/Support] Don't crash on empty nullptr ranges when decoding LEBs

Summary:
If the decoding functions are called with both start and end pointers
being nullptr, the function will crash due to a nullptr dereference.
This happens because the function does not recognise nullptr as a valid
end pointer.

Obviously, nobody is going to pass null pointers here deliberately, but
it can happen indirectly (as it did for me), when calling these
functions on an ArrayRef, as a default-initialized empty ArrayRef will
have both begin() and end() pointers equal to nullptr.

The fix is to simply remove the nullptr check. Passing nullptr for "end"
with a valid "begin" pointer will still work, as one cannot reach
nullptr by incrementing a valid pointer without triggerring UB.

Reviewers: dblaikie

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D77304
This commit is contained in:
Pavel Labath 2020-04-02 14:48:12 +02:00
parent cd3ee0b013
commit ae3c34854e
2 changed files with 8 additions and 2 deletions

View File

@ -134,7 +134,7 @@ inline uint64_t decodeULEB128(const uint8_t *p, unsigned *n = nullptr,
if (error)
*error = nullptr;
do {
if (end && p == end) {
if (p == end) {
if (error)
*error = "malformed uleb128, extends past end";
if (n)
@ -168,7 +168,7 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
if (error)
*error = nullptr;
do {
if (end && p == end) {
if (p == end) {
if (error)
*error = "malformed sleb128, extends past end";
if (n)

View File

@ -113,6 +113,9 @@ TEST(LEB128Test, DecodeULEB128) {
EXPECT_EQ(EXPECTED, Actual); \
} while (0)
// Don't crash
EXPECT_EQ(0u, decodeULEB128(nullptr, nullptr, nullptr));
// Decode ULEB128
EXPECT_DECODE_ULEB128_EQ(0u, "\x00");
EXPECT_DECODE_ULEB128_EQ(1u, "\x01");
@ -148,6 +151,9 @@ TEST(LEB128Test, DecodeSLEB128) {
EXPECT_EQ(EXPECTED, Actual); \
} while (0)
// Don't crash
EXPECT_EQ(0, decodeSLEB128(nullptr, nullptr, nullptr));
// Decode SLEB128
EXPECT_DECODE_SLEB128_EQ(0L, "\x00");
EXPECT_DECODE_SLEB128_EQ(1L, "\x01");