[Lldb-commits] [lldb] [LLDB] Add an assert to verify sign_bit_pos is within the valid range (NFC) (PR #95678)

Shivam Gupta via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 21 00:40:55 PDT 2024


https://github.com/xgupta updated https://github.com/llvm/llvm-project/pull/95678

>From c02fa111c62f98a20055745d5d8b75aea8fd748a Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Sun, 16 Jun 2024 00:21:51 +0530
Subject: [PATCH 1/2] [LLDB] Add an assert to verify sign_bit_pos is within the
 valid range

---
 lldb/source/Utility/Scalar.cpp | 31 +++++++++++++++----------------
 1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index c680101aa9efa..6e2f1ca4c1613 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -745,26 +745,25 @@ Status Scalar::SetValueFromData(const DataExtractor &data,
 
 bool Scalar::SignExtend(uint32_t sign_bit_pos) {
   const uint32_t max_bit_pos = GetByteSize() * 8;
+  assert(sign_bit_pos < max_bit_pos);
 
-  if (sign_bit_pos < max_bit_pos) {
-    switch (m_type) {
-    case Scalar::e_void:
-    case Scalar::e_float:
-      return false;
+  switch (m_type) {
+  case Scalar::e_void:
+  case Scalar::e_float:
+    return false;
 
-    case Scalar::e_int:
-      if (sign_bit_pos < (max_bit_pos - 1)) {
-        llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
-        llvm::APInt bitwize_and = m_integer & sign_bit;
-        if (bitwize_and.getBoolValue()) {
-          llvm::APInt mask =
-              ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
-          m_integer |= APSInt(std::move(mask), m_integer.isUnsigned());
-        }
-        return true;
+  case Scalar::e_int:
+    if (sign_bit_pos < (max_bit_pos - 1)) {
+      llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
+      llvm::APInt bitwize_and = m_integer & sign_bit;
+      if (bitwize_and.getBoolValue()) {
+        llvm::APInt mask =
+            ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
+        m_integer |= APSInt(std::move(mask), m_integer.isUnsigned());
       }
-      break;
+      return true;
     }
+    break;
   }
   return false;
 }

>From fc09409c733a4f5c92fdbb30cb6e23858bf1e49a Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Wed, 19 Jun 2024 13:57:55 +0530
Subject: [PATCH 2/2] address review suggestion

---
 lldb/source/Utility/Scalar.cpp | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/lldb/source/Utility/Scalar.cpp b/lldb/source/Utility/Scalar.cpp
index 6e2f1ca4c1613..496f402a74114 100644
--- a/lldb/source/Utility/Scalar.cpp
+++ b/lldb/source/Utility/Scalar.cpp
@@ -747,25 +747,17 @@ bool Scalar::SignExtend(uint32_t sign_bit_pos) {
   const uint32_t max_bit_pos = GetByteSize() * 8;
   assert(sign_bit_pos < max_bit_pos);
 
-  switch (m_type) {
-  case Scalar::e_void:
-  case Scalar::e_float:
+  if (m_type != Scalar::e_int || sign_bit_pos >= (max_bit_pos - 1)) {
     return false;
+  }
 
-  case Scalar::e_int:
-    if (sign_bit_pos < (max_bit_pos - 1)) {
-      llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
-      llvm::APInt bitwize_and = m_integer & sign_bit;
-      if (bitwize_and.getBoolValue()) {
-        llvm::APInt mask =
-            ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
-        m_integer |= APSInt(std::move(mask), m_integer.isUnsigned());
-      }
-      return true;
-    }
-    break;
+  llvm::APInt sign_bit = llvm::APInt::getSignMask(sign_bit_pos + 1);
+  llvm::APInt bitwize_and = m_integer & sign_bit;
+  if (bitwize_and.getBoolValue()) {
+    llvm::APInt mask = ~(sign_bit) + llvm::APInt(m_integer.getBitWidth(), 1);
+    m_integer |= APSInt(std::move(mask), m_integer.isUnsigned());
   }
-  return false;
+  return true;
 }
 
 size_t Scalar::GetAsMemoryData(void *dst, size_t dst_len,



More information about the lldb-commits mailing list