[Lldb-commits] [PATCH] D108717: Fix Reference case for TypeSystemClang::GetChildCompilerTypeAtIndex(...) to avoid possible invalid cast

Shafik Yaghmour via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Wed Aug 25 11:29:07 PDT 2021


shafik created this revision.
shafik added reviewers: aprantl, teemperor, werat.
Herald added a subscriber: arphaman.
shafik requested review of this revision.

D103532 <https://reviews.llvm.org/D103532> modified this case to preserve type sugar but we can end up with cases where the cast is not valid. I modified the code to use `GetLValueReferenceType(type)`/`GetRValueReferenceType(type)` respectively.

In the case being tested in the test case we end with the following type:

  TypedefType 0x7f8a710202f0 'std::__compressed_pair_elem<struct std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::__rep, 0, false>::const_reference' sugar
  |-Typedef 0x7f8a71020280 'const_reference'
  `-LValueReferenceType 0x7f8a71020250 'const struct std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> >::__rep &'
  ...

which can't be cast to `ReferenceType`.


https://reviews.llvm.org/D108717

Files:
  lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
  lldb/test/API/lang/cpp/null_references/Makefile
  lldb/test/API/lang/cpp/null_references/TestNullReferences.py
  lldb/test/API/lang/cpp/null_references/main.cpp


Index: lldb/test/API/lang/cpp/null_references/main.cpp
===================================================================
--- /dev/null
+++ lldb/test/API/lang/cpp/null_references/main.cpp
@@ -0,0 +1,12 @@
+#include <string>
+
+int f(std::string &instr) {
+  return instr.size(); // break here
+}
+
+int main() {
+  std::string *bad_str = (std::string *)nullptr;
+  // This is undefined behavior. We are purposefully trying to hit
+  // GetCrashingDereference(...)
+  return f(*bad_str);
+}
Index: lldb/test/API/lang/cpp/null_references/TestNullReferences.py
===================================================================
--- /dev/null
+++ lldb/test/API/lang/cpp/null_references/TestNullReferences.py
@@ -0,0 +1,14 @@
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class TestNullReferences(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+
+    def test(self):
+        self.build()
+        lldbutil.run_to_source_breakpoint(self, "// break here", lldb.SBFileSpec("main.cpp"))
+
+        self.runCmd("continue")
Index: lldb/test/API/lang/cpp/null_references/Makefile
===================================================================
--- /dev/null
+++ lldb/test/API/lang/cpp/null_references/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+
+include Makefile.rules
Index: lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
===================================================================
--- lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -6502,10 +6502,13 @@
   case clang::Type::LValueReference:
   case clang::Type::RValueReference:
     if (idx_is_valid) {
-      const clang::ReferenceType *reference_type =
-          llvm::cast<clang::ReferenceType>(GetQualType(type).getTypePtr());
-      CompilerType pointee_clang_type =
-          GetType(reference_type->getPointeeType());
+      CompilerType pointee_clang_type;
+
+      if (parent_type_class == clang::Type::LValueReference)
+        pointee_clang_type = GetLValueReferenceType(type).GetPointeeType();
+      else
+        pointee_clang_type = GetRValueReferenceType(type).GetPointeeType();
+
       if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
         child_is_deref_of_parent = false;
         bool tmp_child_is_deref_of_parent = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108717.368682.patch
Type: text/x-patch
Size: 2416 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210825/657c6fe9/attachment-0001.bin>


More information about the lldb-commits mailing list