llvm.org GIT mirror llvm / cd63a63
[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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364169 91177308-0d34-0410-b5e6-96231b3b80d8 Pavel Labath 3 months ago
2 changed file(s) with 24 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
156156 byte = Data[offset++];
157157 result |= uint64_t(byte & 0x7f) << shift;
158158 shift += 7;
159 if ((byte & 0x80) == 0)
160 break;
159 if ((byte & 0x80) == 0) {
160 *offset_ptr = offset;
161 return result;
162 }
161163 }
162
163 *offset_ptr = offset;
164 return result;
164 return 0;
165165 }
166166
167167 int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
177177 byte = Data[offset++];
178178 result |= uint64_t(byte & 0x7f) << shift;
179179 shift += 7;
180 if ((byte & 0x80) == 0)
181 break;
180 if ((byte & 0x80) == 0) {
181 // Sign bit of byte is 2nd high order bit (0x40)
182 if (shift < 64 && (byte & 0x40))
183 result |= -(1ULL << shift);
184
185 *offset_ptr = offset;
186 return result;
187 }
182188 }
183
184 // Sign bit of byte is 2nd high order bit (0x40)
185 if (shift < 64 && (byte & 0x40))
186 result |= -(1ULL << shift);
187
188 *offset_ptr = offset;
189 return result;
189 return 0;
190190 }
115115 EXPECT_EQ(8U, offset);
116116 }
117117
118 TEST(DataExtractorTest, LEB128_error) {
119 DataExtractor DE(StringRef("\x81"), false, 8);
120 uint32_t Offset = 0;
121 EXPECT_EQ(0U, DE.getULEB128(&Offset));
122 EXPECT_EQ(0U, Offset);
123
124 Offset = 0;
125 EXPECT_EQ(0U, DE.getSLEB128(&Offset));
126 EXPECT_EQ(0U, Offset);
118127 }
128 }