Author: Adrian Prantl Date: 2023-04-21T08:21:56-07:00 New Revision: 737820e6d6e2df50f2ddf522a0db9ffb794ff749
URL: https://github.com/llvm/llvm-project/commit/737820e6d6e2df50f2ddf522a0db9ffb794ff749 DIFF: https://github.com/llvm/llvm-project/commit/737820e6d6e2df50f2ddf522a0db9ffb794ff749.diff LOG: Make diagnostics API safer to use I received a crash report in DiagnosticManager that was caused 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 Differential Revision: https://reviews.llvm.org/D148823 Added: Modified: lldb/include/lldb/Expression/DiagnosticManager.h lldb/unittests/Expression/DiagnosticManagerTest.cpp Removed: ################################################################################ diff --git a/lldb/include/lldb/Expression/DiagnosticManager.h b/lldb/include/lldb/Expression/DiagnosticManager.h index c0271c954ba3f..df9ba3b245f51 100644 --- a/lldb/include/lldb/Expression/DiagnosticManager.h +++ b/lldb/include/lldb/Expression/DiagnosticManager.h @@ -114,7 +114,8 @@ class DiagnosticManager { } 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, ...) diff --git a/lldb/unittests/Expression/DiagnosticManagerTest.cpp b/lldb/unittests/Expression/DiagnosticManagerTest.cpp index 3cfb5ed0b66bb..cab26debedb14 100644 --- a/lldb/unittests/Expression/DiagnosticManagerTest.cpp +++ b/lldb/unittests/Expression/DiagnosticManagerTest.cpp @@ -75,6 +75,9 @@ TEST(DiagnosticManagerTest, HasFixits) { 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) { _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits