diff --git a/lldb/include/lldb/Utility/DataExtractor.h b/lldb/include/lldb/Utility/DataExtractor.h --- a/lldb/include/lldb/Utility/DataExtractor.h +++ b/lldb/include/lldb/Utility/DataExtractor.h @@ -535,13 +535,13 @@ uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const; - /// Extract an signed integer of size \a byte_size from \a *offset_ptr, then - /// extract and signe extend the bitfield from this value if \a + /// Extract an signed integer of size \a size from \a *offset_ptr, then + /// extract and sign-extend the bitfield from this value if \a /// bitfield_bit_size is non-zero. /// - /// Extract a single signed integer value (sign extending if required) and + /// Extract a single signed integer value (sign-extending if required) and /// update the offset pointed to by \a offset_ptr. The size of the extracted - /// integer is specified by the \a byte_size argument. \a byte_size must + /// integer is specified by the \a size argument. \a size must /// have a value greater than or equal to one and less than or equal to /// eight since the return value is 64 bits wide. /// diff --git a/lldb/source/Utility/DataExtractor.cpp b/lldb/source/Utility/DataExtractor.cpp --- a/lldb/source/Utility/DataExtractor.cpp +++ b/lldb/source/Utility/DataExtractor.cpp @@ -604,6 +604,8 @@ int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const { + assert(size >= 1 && "GetMaxS64Bitfield size must be >= 1"); + assert(size <= 8 && "GetMaxS64Bitfield size must be <= 8"); int64_t sval64 = GetMaxS64(offset_ptr, size); if (bitfield_bit_size > 0) { int32_t lsbcount = bitfield_bit_offset; @@ -612,7 +614,7 @@ if (lsbcount > 0) sval64 >>= lsbcount; uint64_t bitfield_mask = - ((static_cast(1)) << bitfield_bit_size) - 1; + llvm::maskTrailingOnes(bitfield_bit_size); sval64 &= bitfield_mask; // sign extend if needed if (sval64 & ((static_cast(1)) << (bitfield_bit_size - 1))) diff --git a/lldb/unittests/Utility/DataExtractorTest.cpp b/lldb/unittests/Utility/DataExtractorTest.cpp --- a/lldb/unittests/Utility/DataExtractorTest.cpp +++ b/lldb/unittests/Utility/DataExtractorTest.cpp @@ -25,6 +25,9 @@ offset = 0; ASSERT_EQ(buffer[1], BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 8, 8)); offset = 0; + ASSERT_EQ(static_cast(0xEFCDAB8967452301), + LE.GetMaxU64Bitfield(&offset, sizeof(buffer), 64, 0)); + offset = 0; ASSERT_EQ(static_cast(0x0123456789ABCDEF), BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 64, 0)); offset = 0; @@ -40,6 +43,12 @@ offset = 0; ASSERT_EQ(int8_t(buffer[1]), BE.GetMaxS64Bitfield(&offset, sizeof(buffer), 8, 8)); + offset = 0; + ASSERT_EQ(static_cast(0xEFCDAB8967452301), + LE.GetMaxS64Bitfield(&offset, sizeof(buffer), 64, 0)); + offset = 0; + ASSERT_EQ(static_cast(0x0123456789ABCDEF), + BE.GetMaxS64Bitfield(&offset, sizeof(buffer), 64, 0)); } TEST(DataExtractorTest, PeekData) {