Skip to content

Commit 1dd9162

Browse files
authored
[lldb] Fix a crasher when using the public API. (#80508)
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 and it caused a crash..
1 parent c8a97c0 commit 1dd9162

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lldb/source/Target/TargetList.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,13 @@ void TargetList::SetSelectedTarget(uint32_t index) {
532532
}
533533

534534
void TargetList::SetSelectedTarget(const TargetSP &target_sp) {
535-
std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
536-
auto it = llvm::find(m_target_list, target_sp);
537-
SetSelectedTargetInternal(std::distance(m_target_list.begin(), it));
535+
// Don't allow an invalid target shared pointer or a target that has been
536+
// destroyed to become the selected target.
537+
if (target_sp && target_sp->IsValid()) {
538+
std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex);
539+
auto it = llvm::find(m_target_list, target_sp);
540+
SetSelectedTargetInternal(std::distance(m_target_list.begin(), it));
541+
}
538542
}
539543

540544
lldb::TargetSP TargetList::GetSelectedTarget() {

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)