[llvm] r364169 - [Support] Fix error handling in DataExtractor::get[US]LEB128

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 24 02:11:25 PDT 2019


Author: labath
Date: Mon Jun 24 02:11:24 2019
New Revision: 364169

URL: http://llvm.org/viewvc/llvm-project?rev=364169&view=rev
Log:
[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

Modified:
    llvm/trunk/lib/Support/DataExtractor.cpp
    llvm/trunk/unittests/Support/DataExtractorTest.cpp

Modified: llvm/trunk/lib/Support/DataExtractor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/DataExtractor.cpp?rev=364169&r1=364168&r2=364169&view=diff
==============================================================================
--- llvm/trunk/lib/Support/DataExtractor.cpp (original)
+++ llvm/trunk/lib/Support/DataExtractor.cpp Mon Jun 24 02:11:24 2019
@@ -157,12 +157,12 @@ uint64_t DataExtractor::getULEB128(uint3
     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
     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;
 }

Modified: llvm/trunk/unittests/Support/DataExtractorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/DataExtractorTest.cpp?rev=364169&r1=364168&r2=364169&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/DataExtractorTest.cpp (original)
+++ llvm/trunk/unittests/Support/DataExtractorTest.cpp Mon Jun 24 02:11:24 2019
@@ -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);
+}
 }




More information about the llvm-commits mailing list