[Lldb-commits] [PATCH] D148823: Make diagnostics API safer to use

Adrian Prantl via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 20 11:19:47 PDT 2023


aprantl created this revision.
aprantl added reviewers: kastiglione, JDevlieghere, Michael137.
Herald added a project: All.
aprantl requested review of this revision.

I received a crash report in DiagnosticManager that was causes by a nullptr diagnostic having been added. The API allows passing in a null unique_ptr, but all the methods are written assuming that all pointers a dereferencable. This patch makes it impossible to add a null diagnostic.

rdar://107633615


https://reviews.llvm.org/D148823

Files:
  lldb/include/lldb/Expression/DiagnosticManager.h
  lldb/unittests/Expression/DiagnosticManagerTest.cpp


Index: lldb/unittests/Expression/DiagnosticManagerTest.cpp
===================================================================
--- lldb/unittests/Expression/DiagnosticManagerTest.cpp
+++ lldb/unittests/Expression/DiagnosticManagerTest.cpp
@@ -75,6 +75,9 @@
 TEST(DiagnosticManagerTest, GetStringNoDiags) {
   DiagnosticManager mgr;
   EXPECT_EQ("", mgr.GetString());
+  std::unique_ptr<Diagnostic> empty;
+  mgr.AddDiagnostic(std::move(empty));
+  EXPECT_EQ("", mgr.GetString());
 }
 
 TEST(DiagnosticManagerTest, GetStringBasic) {
Index: lldb/include/lldb/Expression/DiagnosticManager.h
===================================================================
--- lldb/include/lldb/Expression/DiagnosticManager.h
+++ lldb/include/lldb/Expression/DiagnosticManager.h
@@ -114,7 +114,8 @@
   }
 
   void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
-    m_diagnostics.push_back(std::move(diagnostic));
+    if (diagnostic)
+      m_diagnostics.push_back(std::move(diagnostic));
   }
 
   size_t Printf(DiagnosticSeverity severity, const char *format, ...)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148823.515400.patch
Type: text/x-patch
Size: 1060 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20230420/25677557/attachment.bin>


More information about the lldb-commits mailing list