[Lldb-commits] [PATCH] D20355: Avoid an assertion failure when a bit field is extracted from a value of the same size.

Bryan Chan via lldb-commits lldb-commits at lists.llvm.org
Wed May 18 15:47:23 PDT 2016


bryanpkc updated this revision to Diff 57695.
bryanpkc added a comment.

Added a unit test in ScalarTest.cpp that catches this particular error.


http://reviews.llvm.org/D20355

Files:
  source/Core/Scalar.cpp
  unittests/Core/ScalarTest.cpp

Index: unittests/Core/ScalarTest.cpp
===================================================================
--- unittests/Core/ScalarTest.cpp
+++ unittests/Core/ScalarTest.cpp
@@ -79,3 +79,27 @@
     ASSERT_EQ((unsigned long long)a, a_scalar.ULongLong());
 }
 
+TEST(ScalarTest, ExtractBitfield)
+{
+    uint32_t len = sizeof(long long) * 8;
+
+    long long a1 = 0xf1f2f3f4f5f6f7f8LL;
+    long long b1 = 0xff1f2f3f4f5f6f7fLL;
+    Scalar s_scalar(a1);
+    ASSERT_TRUE(s_scalar.ExtractBitfield(0, 0));
+    ASSERT_EQ(0, memcmp(&a1, s_scalar.GetBytes(), sizeof(a1)));
+    ASSERT_TRUE(s_scalar.ExtractBitfield(len, 0));
+    ASSERT_EQ(0, memcmp(&a1, s_scalar.GetBytes(), sizeof(a1)));
+    ASSERT_TRUE(s_scalar.ExtractBitfield(len - 4, 4));
+    ASSERT_EQ(0, memcmp(&b1, s_scalar.GetBytes(), sizeof(b1)));
+
+    unsigned long long a2 = 0xf1f2f3f4f5f6f7f8ULL;
+    unsigned long long b2 = 0x0f1f2f3f4f5f6f7fULL;
+    Scalar u_scalar(a2);
+    ASSERT_TRUE(u_scalar.ExtractBitfield(0, 0));
+    ASSERT_EQ(0, memcmp(&a2, u_scalar.GetBytes(), sizeof(a2)));
+    ASSERT_TRUE(u_scalar.ExtractBitfield(len, 0));
+    ASSERT_EQ(0, memcmp(&a2, u_scalar.GetBytes(), sizeof(a2)));
+    ASSERT_TRUE(u_scalar.ExtractBitfield(len - 4, 4));
+    ASSERT_EQ(0, memcmp(&b2, u_scalar.GetBytes(), sizeof(b2)));
+}
Index: source/Core/Scalar.cpp
===================================================================
--- source/Core/Scalar.cpp
+++ source/Core/Scalar.cpp
@@ -2788,15 +2788,15 @@
         case Scalar::e_slonglong:
         case Scalar::e_sint128:
         case Scalar::e_sint256:
-            m_integer = m_integer.ashr(bit_offset).trunc(bit_size).sext(8 * GetByteSize());
+            m_integer = m_integer.ashr(bit_offset).sextOrTrunc(bit_size).sextOrSelf(8 * GetByteSize());
             return true;
 
         case Scalar::e_uint:
         case Scalar::e_ulong:
         case Scalar::e_ulonglong:
         case Scalar::e_uint128:
         case Scalar::e_uint256:
-            m_integer = m_integer.lshr(bit_offset).trunc(bit_size).zext(8 * GetByteSize());
+            m_integer = m_integer.lshr(bit_offset).zextOrTrunc(bit_size).zextOrSelf(8 * GetByteSize());
             return true;
     }
     return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20355.57695.patch
Type: text/x-patch
Size: 2207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20160518/1ba4b291/attachment.bin>


More information about the lldb-commits mailing list