diff options
| author | Jonas Devlieghere <jonas@devlieghere.com> | 2025-11-20 16:45:11 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-20 16:45:11 -0800 |
| commit | 06eac9feb92cba1d24e8a674c643aae1200d2bc8 (patch) | |
| tree | 7acd1a4cc8ce631a5928c2deea7b3f95656777a8 /lldb/source/Commands/CommandObjectSource.cpp | |
| parent | ac55d7859fc53d9ca8444a29a93fc45d16ecb26a (diff) | |
[lldb] Eliminate SupportFileSP nullptr derefs (#168624)
This patch fixes and eliminates the possibility of SupportFileSP ever
being nullptr. The support file was originally treated like a value
type, but became a polymorphic type and therefore has to be stored and
passed around as a pointer.
To avoid having all the callers check the validity of the pointer, I
introduced the invariant that SupportFileSP is never null and always
default constructed. However, without enforcement at the type level,
that's fragile and indeed, we already identified two crashes where
someone accidentally broke that invariant.
This PR introduces a NonNullSharedPtr to prevent that. NonNullSharedPtr
is a smart pointer wrapper around std::shared_ptr that guarantees the
pointer is never null. If default-constructed, it creates a
default-constructed instance of the contained type. Note that I'm using
private inheritance because you shouldn't inherit from standard library
classes due to the lack of virtual destructor. So while the new
abstraction looks like a `std::shared_ptr`, it is in fact **not** a
shared pointer. Given that our destructor is trivial, we could use
public inheritance, but currently there's no need for it.
rdar://164989579
Diffstat (limited to 'lldb/source/Commands/CommandObjectSource.cpp')
| -rw-r--r-- | lldb/source/Commands/CommandObjectSource.cpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/lldb/source/Commands/CommandObjectSource.cpp b/lldb/source/Commands/CommandObjectSource.cpp index 0b4599b16ef0..c9835e74ac51 100644 --- a/lldb/source/Commands/CommandObjectSource.cpp +++ b/lldb/source/Commands/CommandObjectSource.cpp @@ -777,7 +777,7 @@ protected: if (sc.function) { Target &target = GetTarget(); - SupportFileSP start_file = std::make_shared<SupportFile>(); + SupportFileNSP start_file = std::make_shared<SupportFile>(); uint32_t start_line; uint32_t end_line; FileSpec end_file; @@ -1194,7 +1194,7 @@ protected: // file(s) will be found and assigned to // sc.comp_unit->GetPrimarySupportFile, which is NOT what we want to // print. Instead, we want to print the one from the line entry. - lldb::SupportFileSP found_file_sp = sc.line_entry.file_sp; + SupportFileNSP found_file_sp = sc.line_entry.file_sp; target.GetSourceManager().DisplaySourceLinesWithLineNumbers( found_file_sp, m_options.start_line, column, 0, |
