summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Support/DataExtractor.cpp28
-rw-r--r--llvm/unittests/Support/DataExtractorTest.cpp10
2 files changed, 24 insertions, 14 deletions
diff --git a/llvm/lib/Support/DataExtractor.cpp b/llvm/lib/Support/DataExtractor.cpp
index 6328d779b3d..18e1423b546 100644
--- a/llvm/lib/Support/DataExtractor.cpp
+++ b/llvm/lib/Support/DataExtractor.cpp
@@ -157,12 +157,12 @@ uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
byte = Data[offset++];
result |= uint64_t(byte & 0x7f) << shift;
shift += 7;
- if ((byte & 0x80) == 0)
- break;
+ if ((byte & 0x80) == 0) {
+ *offset_ptr = offset;
+ return result;
+ }
}
-
- *offset_ptr = offset;
- return result;
+ return 0;
}
int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
@@ -178,14 +178,14 @@ int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
byte = Data[offset++];
result |= uint64_t(byte & 0x7f) << shift;
shift += 7;
- if ((byte & 0x80) == 0)
- break;
+ if ((byte & 0x80) == 0) {
+ // Sign bit of byte is 2nd high order bit (0x40)
+ if (shift < 64 && (byte & 0x40))
+ result |= -(1ULL << shift);
+
+ *offset_ptr = offset;
+ return result;
+ }
}
-
- // Sign bit of byte is 2nd high order bit (0x40)
- if (shift < 64 && (byte & 0x40))
- result |= -(1ULL << shift);
-
- *offset_ptr = offset;
- return result;
+ return 0;
}
diff --git a/llvm/unittests/Support/DataExtractorTest.cpp b/llvm/unittests/Support/DataExtractorTest.cpp
index 5663dad5cb6..9726a74d81f 100644
--- a/llvm/unittests/Support/DataExtractorTest.cpp
+++ b/llvm/unittests/Support/DataExtractorTest.cpp
@@ -116,4 +116,14 @@ TEST(DataExtractorTest, LEB128) {
EXPECT_EQ(8U, offset);
}
+TEST(DataExtractorTest, LEB128_error) {
+ DataExtractor DE(StringRef("\x81"), false, 8);
+ uint32_t Offset = 0;
+ EXPECT_EQ(0U, DE.getULEB128(&Offset));
+ EXPECT_EQ(0U, Offset);
+
+ Offset = 0;
+ EXPECT_EQ(0U, DE.getSLEB128(&Offset));
+ EXPECT_EQ(0U, Offset);
+}
}
OpenPOWER on IntegriCloud