Author: Greg Clayton Date: 2024-02-06T13:53:29-08:00 New Revision: 1dd9162b95d29367635f32c7be24779b1ddaa7e5
URL: https://github.com/llvm/llvm-project/commit/1dd9162b95d29367635f32c7be24779b1ddaa7e5 DIFF: https://github.com/llvm/llvm-project/commit/1dd9162b95d29367635f32c7be24779b1ddaa7e5.diff LOG: [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.. Added: Modified: lldb/source/Target/TargetList.cpp lldb/test/API/python_api/target/TestTargetAPI.py Removed: ################################################################################ diff --git a/lldb/source/Target/TargetList.cpp b/lldb/source/Target/TargetList.cpp index 121b6253d2a59d..b5d308739d0fac 100644 --- a/lldb/source/Target/TargetList.cpp +++ b/lldb/source/Target/TargetList.cpp @@ -532,9 +532,13 @@ void TargetList::SetSelectedTarget(uint32_t index) { } void TargetList::SetSelectedTarget(const TargetSP &target_sp) { - std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); - auto it = llvm::find(m_target_list, target_sp); - SetSelectedTargetInternal(std::distance(m_target_list.begin(), it)); + // Don't allow an invalid target shared pointer or a target that has been + // destroyed to become the selected target. + if (target_sp && target_sp->IsValid()) { + std::lock_guard<std::recursive_mutex> guard(m_target_list_mutex); + auto it = llvm::find(m_target_list, target_sp); + SetSelectedTargetInternal(std::distance(m_target_list.begin(), it)); + } } lldb::TargetSP TargetList::GetSelectedTarget() { diff --git a/lldb/test/API/python_api/target/TestTargetAPI.py b/lldb/test/API/python_api/target/TestTargetAPI.py index c8e1904428c8aa..63d34340a8836e 100644 --- a/lldb/test/API/python_api/target/TestTargetAPI.py +++ b/lldb/test/API/python_api/target/TestTargetAPI.py @@ -526,3 +526,9 @@ def test_is_loaded(self): target.IsLoaded(module), "Running the target should " "have loaded its modules.", ) + + @no_debug_info_test + def test_setting_selected_target_with_invalid_target(self): + """Make sure we don't crash when trying to select invalid target.""" + target = lldb.SBTarget() + self.dbg.SetSelectedTarget(target) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits