Skip to content

Commit 737820e

Browse files
committed
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
1 parent 6c9066f commit 737820e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lldb/include/lldb/Expression/DiagnosticManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ class DiagnosticManager {
114114
}
115115

116116
void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
117-
m_diagnostics.push_back(std::move(diagnostic));
117+
if (diagnostic)
118+
m_diagnostics.push_back(std::move(diagnostic));
118119
}
119120

120121
size_t Printf(DiagnosticSeverity severity, const char *format, ...)

lldb/unittests/Expression/DiagnosticManagerTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ TEST(DiagnosticManagerTest, HasFixits) {
7575
TEST(DiagnosticManagerTest, GetStringNoDiags) {
7676
DiagnosticManager mgr;
7777
EXPECT_EQ("", mgr.GetString());
78+
std::unique_ptr<Diagnostic> empty;
79+
mgr.AddDiagnostic(std::move(empty));
80+
EXPECT_EQ("", mgr.GetString());
7881
}
7982

8083
TEST(DiagnosticManagerTest, GetStringBasic) {

0 commit comments

Comments
 (0)