[Lldb-commits] [PATCH] D70992: Replacing to use of ul suffix in GetMaxU64Bitfield since it not guarenteed to be 64 bit

Shafik Yaghmour via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Dec 5 10:05:44 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rGfffd70291e12: [LLDB] Replacing use of ul suffix in GetMaxU64Bitfield since it not guarenteed… (authored by shafik).
Herald added a project: LLDB.

Changed prior to commit:
  https://reviews.llvm.org/D70992?vs=232218&id=232380#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70992/new/

https://reviews.llvm.org/D70992

Files:
  lldb/source/Utility/DataExtractor.cpp
  lldb/unittests/Utility/DataExtractorTest.cpp


Index: lldb/unittests/Utility/DataExtractorTest.cpp
===================================================================
--- lldb/unittests/Utility/DataExtractorTest.cpp
+++ lldb/unittests/Utility/DataExtractorTest.cpp
@@ -13,7 +13,7 @@
 using namespace lldb_private;
 
 TEST(DataExtractorTest, GetBitfield) {
-  uint8_t buffer[] = {0x01, 0x23, 0x45, 0x67};
+  uint8_t buffer[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
   DataExtractor LE(buffer, sizeof(buffer), lldb::eByteOrderLittle,
                    sizeof(void *));
   DataExtractor BE(buffer, sizeof(buffer), lldb::eByteOrderBig, sizeof(void *));
@@ -24,6 +24,15 @@
   ASSERT_EQ(buffer[1], LE.GetMaxU64Bitfield(&offset, sizeof(buffer), 8, 8));
   offset = 0;
   ASSERT_EQ(buffer[1], BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 8, 8));
+  offset = 0;
+  ASSERT_EQ(static_cast<uint64_t>(0x0123456789ABCDEF),
+            BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 64, 0));
+  offset = 0;
+  ASSERT_EQ(static_cast<uint64_t>(0x01234567),
+            BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 32, 0));
+  offset = 0;
+  ASSERT_EQ(static_cast<uint64_t>(0x012345678),
+            BE.GetMaxU64Bitfield(&offset, sizeof(buffer), 36, 0));
 
   offset = 0;
   ASSERT_EQ(int8_t(buffer[1]),
Index: lldb/source/Utility/DataExtractor.cpp
===================================================================
--- lldb/source/Utility/DataExtractor.cpp
+++ lldb/source/Utility/DataExtractor.cpp
@@ -577,18 +577,28 @@
 uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size,
                                           uint32_t bitfield_bit_size,
                                           uint32_t bitfield_bit_offset) const {
+  assert(bitfield_bit_size <= 64);
   uint64_t uval64 = GetMaxU64(offset_ptr, size);
-  if (bitfield_bit_size > 0) {
-    int32_t lsbcount = bitfield_bit_offset;
-    if (m_byte_order == eByteOrderBig)
-      lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
-    if (lsbcount > 0)
-      uval64 >>= lsbcount;
-    uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
-    if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
-      return uval64;
-    uval64 &= bitfield_mask;
-  }
+
+  if (bitfield_bit_size == 0)
+    return uval64;
+
+  int32_t lsbcount = bitfield_bit_offset;
+  if (m_byte_order == eByteOrderBig)
+    lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
+
+  if (lsbcount > 0)
+    uval64 >>= lsbcount;
+
+  uint64_t bitfield_mask =
+      (bitfield_bit_size == 64
+           ? std::numeric_limits<uint64_t>::max()
+           : ((static_cast<uint64_t>(1) << bitfield_bit_size) - 1));
+  if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
+    return uval64;
+
+  uval64 &= bitfield_mask;
+
   return uval64;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70992.232380.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20191205/006494de/attachment.bin>


More information about the lldb-commits mailing list