Skip to content

Commit b988ef4

Browse files
committed
Fix a crasher when using the public API.
A user found a crash when they would do code like: (lldb) script >>> target = lldb.SBTarget() >>> lldb.debugger.SetSelectedTarget(target) We were not checking if the target was valid in SBDebugger::SetSelectedTarget(...).
1 parent 9258f3e commit b988ef4

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

lldb/source/API/SBDebugger.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,9 @@ void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
10891089
Log *log = GetLog(LLDBLog::API);
10901090

10911091
TargetSP target_sp(sb_target.GetSP());
1092-
if (m_opaque_sp) {
1092+
if (m_opaque_sp && target_sp)
10931093
m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp);
1094-
}
1094+
10951095
if (log) {
10961096
SBStream sstr;
10971097
sb_target.GetDescription(sstr, eDescriptionLevelBrief);
@@ -1704,20 +1704,20 @@ SBDebugger::LoadTraceFromFile(SBError &error,
17041704

17051705
void SBDebugger::RequestInterrupt() {
17061706
LLDB_INSTRUMENT_VA(this);
1707-
1707+
17081708
if (m_opaque_sp)
1709-
m_opaque_sp->RequestInterrupt();
1709+
m_opaque_sp->RequestInterrupt();
17101710
}
17111711
void SBDebugger::CancelInterruptRequest() {
17121712
LLDB_INSTRUMENT_VA(this);
1713-
1713+
17141714
if (m_opaque_sp)
1715-
m_opaque_sp->CancelInterruptRequest();
1715+
m_opaque_sp->CancelInterruptRequest();
17161716
}
17171717

17181718
bool SBDebugger::InterruptRequested() {
17191719
LLDB_INSTRUMENT_VA(this);
1720-
1720+
17211721
if (m_opaque_sp)
17221722
return m_opaque_sp->InterruptRequested();
17231723
return false;

lldb/test/API/python_api/target/TestTargetAPI.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,9 @@ def test_is_loaded(self):
526526
target.IsLoaded(module),
527527
"Running the target should " "have loaded its modules.",
528528
)
529+
530+
@no_debug_info_test
531+
def test_setting_selected_target_with_invalid_target(self):
532+
"""Make sure we don't crash when trying to select invalid target."""
533+
target = lldb.SBTarget()
534+
self.dbg.SetSelectedTarget(target)

0 commit comments

Comments
 (0)