OSDN Git Service

DataExtractor: use decodeSLEB128 to implement getSLEB128
authorDavid Blaikie <dblaikie@gmail.com>
Mon, 24 Jun 2019 23:45:18 +0000 (23:45 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Mon, 24 Jun 2019 23:45:18 +0000 (23:45 +0000)
Should've been NFC, but turns out DataExtractor had better test coverage
for decoding SLEB128 than the decodeSLEB128 did - revealing a couple of
bugs (one in the error handling, another in sign extension). So fixed
those to get the DataExtractor tests passing again.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364253 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/LEB128.h
lib/Support/DataExtractor.cpp

index eb43c85..0f83723 100644 (file)
@@ -165,6 +165,8 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
   int64_t Value = 0;
   unsigned Shift = 0;
   uint8_t Byte;
+  if (error)
+    *error = nullptr;
   do {
     if (end && p == end) {
       if (error)
@@ -177,8 +179,8 @@ inline int64_t decodeSLEB128(const uint8_t *p, unsigned *n = nullptr,
     Value |= (int64_t(Byte & 0x7f) << Shift);
     Shift += 7;
   } while (Byte >= 128);
-  // Sign extend negative numbers.
-  if (Byte & 0x40)
+  // Sign extend negative numbers if needed.
+  if (Shift < 64 && (Byte & 0x40))
     Value |= (-1ULL) << Shift;
   if (n)
     *n = (unsigned)(p - orig_p);
index 4b26718..673bbb4 100644 (file)
@@ -160,26 +160,15 @@ uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
 }
 
 int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
-  int64_t result = 0;
-  if (Data.empty())
-    return 0;
+  assert(*offset_ptr <= Data.size());
 
-  unsigned shift = 0;
-  uint32_t offset = *offset_ptr;
-  uint8_t byte = 0;
-
-  while (isValidOffset(offset)) {
-    byte = Data[offset++];
-    result |= uint64_t(byte & 0x7f) << shift;
-    shift += 7;
-    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;
-    }
-  }
-  return 0;
+  const char *error;
+  unsigned bytes_read;
+  int64_t result = decodeSLEB128(
+      reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read,
+      reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error);
+  if (error)
+    return 0;
+  *offset_ptr += bytes_read;
+  return result;
 }