Skip to content

Simplify ValueObject::GetQualifiedRepresentationIfAvailable(). #71559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

adrian-prantl
Copy link
Collaborator

I received a couple of nullptr-deref crash reports with no line numbers in this function. The way the function was written it was a bit diffucult to keep track of when result_sp could be null, so this patch simplifies the function to make it more obvious when a nullptr can be contained in the variable.

I received a couple of nullptr-deref crash reports with no line
numbers in this function. The way the function was written it was a
bit diffucult to keep track of when result_sp could be null, so this
patch simplifies the function to make it more obvious when a nullptr
can be contained in the variable.
@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2023

@llvm/pr-subscribers-lldb

Author: Adrian Prantl (adrian-prantl)

Changes

I received a couple of nullptr-deref crash reports with no line numbers in this function. The way the function was written it was a bit diffucult to keep track of when result_sp could be null, so this patch simplifies the function to make it more obvious when a nullptr can be contained in the variable.


Full diff: https://github.com/llvm/llvm-project/pull/71559.diff

1 Files Affected:

  • (modified) lldb/source/Core/ValueObject.cpp (+16-20)
diff --git a/lldb/source/Core/ValueObject.cpp b/lldb/source/Core/ValueObject.cpp
index bdb1bef633d8fb1..a7f7ee64282d891 100644
--- a/lldb/source/Core/ValueObject.cpp
+++ b/lldb/source/Core/ValueObject.cpp
@@ -2594,34 +2594,30 @@ ValueObjectSP ValueObject::CreateConstantValue(ConstString name) {
 
 ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
     lldb::DynamicValueType dynValue, bool synthValue) {
-  ValueObjectSP result_sp(GetSP());
-
+  ValueObjectSP result_sp;
   switch (dynValue) {
   case lldb::eDynamicCanRunTarget:
   case lldb::eDynamicDontRunTarget: {
-    if (!result_sp->IsDynamic()) {
-      if (result_sp->GetDynamicValue(dynValue))
-        result_sp = result_sp->GetDynamicValue(dynValue);
-    }
+    if (!IsDynamic())
+      result_sp = GetDynamicValue(dynValue);
   } break;
   case lldb::eNoDynamicValues: {
-    if (result_sp->IsDynamic()) {
-      if (result_sp->GetStaticValue())
-        result_sp = result_sp->GetStaticValue();
-    }
+    if (IsDynamic())
+      result_sp = GetStaticValue();
   } break;
   }
+  if (!result_sp)
+    result_sp = GetSP();
+  assert(result_sp);
 
-  if (synthValue) {
-    if (!result_sp->IsSynthetic()) {
-      if (result_sp->GetSyntheticValue())
-        result_sp = result_sp->GetSyntheticValue();
-    }
-  } else {
-    if (result_sp->IsSynthetic()) {
-      if (result_sp->GetNonSyntheticValue())
-        result_sp = result_sp->GetNonSyntheticValue();
-    }
+  bool is_synthetic = result_sp->IsSynthetic();
+  if (synthValue && !is_synthetic) {
+    if (auto synth_sp = result_sp->GetSyntheticValue())
+      return synth_sp;
+  }
+  if (!synthValue && is_synthetic) {
+    if (auto non_synth_sp = result_sp->GetNonSyntheticValue())
+      return non_synth_sp;
   }
 
   return result_sp;

@adrian-prantl
Copy link
Collaborator Author

rdar://118018930

Copy link
Member

@Michael137 Michael137 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (result_sp->GetNonSyntheticValue())
result_sp = result_sp->GetNonSyntheticValue();
}
bool is_synthetic = result_sp->IsSynthetic();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const-ify?

@adrian-prantl adrian-prantl merged commit 767ce07 into llvm:main Nov 8, 2023
adrian-prantl added a commit to adrian-prantl/llvm-project that referenced this pull request Nov 8, 2023
…71559)

I received a couple of nullptr-deref crash reports with no line numbers
in this function. The way the function was written it was a bit
diffucult to keep track of when result_sp could be null, so this patch
simplifies the function to make it more obvious when a nullptr can be
contained in the variable.

(cherry picked from commit 767ce07)
adrian-prantl added a commit to swiftlang/llvm-project that referenced this pull request Nov 14, 2023
…25-Simplify-ValueObject-GetQualifiedRepresentationIfAvailable-.-71559

[Cherry-pick into stable/20230725] Simplify ValueObject::GetQualifiedRepresentationIfAvailable(). (llvm#71559)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants