Skip to content

Commit bb6d0b8

Browse files
committedJun 24, 2019
[Support] Fix error handling in DataExtractor::get[US]LEB128
Summary: These functions are documented as not modifying the offset argument if the extraction fails (just like other DataExtractor functions). However, while reviewing D63591 we discovered that this is not the case -- if the function reaches the end of the data buffer, it will just return the value parsed until that point and set offset to point to the end of the buffer. This fixes the functions to act as advertised, and adds a regression test. Reviewers: dblaikie, probinson, bkramer Subscribers: kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63645 llvm-svn: 364169
1 parent a94c18f commit bb6d0b8

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed
 

Diff for: ‎llvm/lib/Support/DataExtractor.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
157157
byte = Data[offset++];
158158
result |= uint64_t(byte & 0x7f) << shift;
159159
shift += 7;
160-
if ((byte & 0x80) == 0)
161-
break;
160+
if ((byte & 0x80) == 0) {
161+
*offset_ptr = offset;
162+
return result;
163+
}
162164
}
163-
164-
*offset_ptr = offset;
165-
return result;
165+
return 0;
166166
}
167167

168168
int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
@@ -178,14 +178,14 @@ int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
178178
byte = Data[offset++];
179179
result |= uint64_t(byte & 0x7f) << shift;
180180
shift += 7;
181-
if ((byte & 0x80) == 0)
182-
break;
181+
if ((byte & 0x80) == 0) {
182+
// Sign bit of byte is 2nd high order bit (0x40)
183+
if (shift < 64 && (byte & 0x40))
184+
result |= -(1ULL << shift);
185+
186+
*offset_ptr = offset;
187+
return result;
188+
}
183189
}
184-
185-
// Sign bit of byte is 2nd high order bit (0x40)
186-
if (shift < 64 && (byte & 0x40))
187-
result |= -(1ULL << shift);
188-
189-
*offset_ptr = offset;
190-
return result;
190+
return 0;
191191
}

Diff for: ‎llvm/unittests/Support/DataExtractorTest.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,14 @@ TEST(DataExtractorTest, LEB128) {
116116
EXPECT_EQ(8U, offset);
117117
}
118118

119+
TEST(DataExtractorTest, LEB128_error) {
120+
DataExtractor DE(StringRef("\x81"), false, 8);
121+
uint32_t Offset = 0;
122+
EXPECT_EQ(0U, DE.getULEB128(&Offset));
123+
EXPECT_EQ(0U, Offset);
124+
125+
Offset = 0;
126+
EXPECT_EQ(0U, DE.getSLEB128(&Offset));
127+
EXPECT_EQ(0U, Offset);
128+
}
119129
}

0 commit comments

Comments
 (0)
Please sign in to comment.