Index: lldb/source/Utility/DataExtractor.cpp =================================================================== --- lldb/source/Utility/DataExtractor.cpp +++ lldb/source/Utility/DataExtractor.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/MD5.h" #include "llvm/Support/MathExtras.h" @@ -877,26 +878,12 @@ if (src == nullptr) return 0; - const uint8_t *end = m_end; - - if (src < end) { - uint64_t result = *src++; - if (result >= 0x80) { - result &= 0x7f; - int shift = 7; - while (src < end) { - uint8_t byte = *src++; - result |= static_cast(byte & 0x7f) << shift; - if ((byte & 0x80) == 0) - break; - shift += 7; - } - } - *offset_ptr = src - m_start; - return result; - } - - return 0; + // The maximum size a 64 bit integer can have when encoded in LEB128. + const std::size_t max_int64_size = 10; + unsigned byte_count = 0; + uint64_t result = llvm::decodeULEB128(src, &byte_count, src + max_int64_size); + *offset_ptr += byte_count; + return result; } // Extracts an signed LEB128 number from this object's data starting at the @@ -910,35 +897,12 @@ if (src == nullptr) return 0; - const uint8_t *end = m_end; - - if (src < end) { - int64_t result = 0; - int shift = 0; - int size = sizeof(int64_t) * 8; - - uint8_t byte = 0; - int bytecount = 0; - - while (src < end) { - bytecount++; - byte = *src++; - result |= static_cast(byte & 0x7f) << shift; - shift += 7; - if ((byte & 0x80) == 0) - break; - } - - // Sign bit of byte is 2nd high order bit (0x40) - if (shift < size && (byte & 0x40)) { - // -(static_cast(1) << 63) errors on the negation with UBSan. - result |= -(static_cast(1) << shift); - } - - *offset_ptr += bytecount; - return result; - } - return 0; + // The maximum size a 64 bit integer can have when encoded in LEB128. + const std::size_t max_int64_size = 10; + unsigned byte_count = 0; + int64_t result = llvm::decodeSLEB128(src, &byte_count, src + max_int64_size); + *offset_ptr += byte_count; + return result; } // Skips a ULEB128 number (signed or unsigned) from this object's data starting