Skip to content

[lldb] Avoid a crash after selecting an unknown platform #151803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2025

Conversation

igorkudrin
Copy link
Collaborator

PlatformList::Create() added an item in the list even if Platform::Create() returned nullptr. Other methods use items of the list without checking, which can lead to a crash. For example:

> lldb
(lldb) platform select unknown
error: unable to find a plug-in for the platform named "unknown"
(lldb) file a.out-arm64
PLEASE submit a bug report to...
Stack dump:
0.      Program arguments: lldb
1.      HandleCommand(command = "file a.out-arm64 ")
...

'PlatformList::Create()' added an item in the list even if
'Platform::Create()' returned 'nullptr'. Other methods use items of the
list without checking, which can lead to a crash. For example:

```
> lldb
(lldb) platform select unkwnown
error: unable to find a plug-in for the platform named "unknown"
(lldb) file a.out-arm64
PLEASE submit a bug report to...
Stack dump:
0.      Program arguments: lldb
1.      HandleCommand(command = "file a.out-arm64 ")
...
```
@llvmbot
Copy link
Member

llvmbot commented Aug 2, 2025

@llvm/pr-subscribers-lldb

Author: Igor Kudrin (igorkudrin)

Changes

PlatformList::Create() added an item in the list even if Platform::Create() returned nullptr. Other methods use items of the list without checking, which can lead to a crash. For example:

> lldb
(lldb) platform select unknown
error: unable to find a plug-in for the platform named "unknown"
(lldb) file a.out-arm64
PLEASE submit a bug report to...
Stack dump:
0.      Program arguments: lldb
1.      HandleCommand(command = "file a.out-arm64 ")
...

Full diff: https://github.com/llvm/llvm-project/pull/151803.diff

2 Files Affected:

  • (modified) lldb/source/Target/Platform.cpp (+2-1)
  • (modified) lldb/unittests/Platform/PlatformTest.cpp (+6)
diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp
index e9d9c8f0976b6..8681adaf5ea76 100644
--- a/lldb/source/Target/Platform.cpp
+++ b/lldb/source/Target/Platform.cpp
@@ -2234,7 +2234,8 @@ PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
 PlatformSP PlatformList::Create(llvm::StringRef name) {
   std::lock_guard<std::recursive_mutex> guard(m_mutex);
   PlatformSP platform_sp = Platform::Create(name);
-  m_platforms.push_back(platform_sp);
+  if (platform_sp)
+    m_platforms.push_back(platform_sp);
   return platform_sp;
 }
 
diff --git a/lldb/unittests/Platform/PlatformTest.cpp b/lldb/unittests/Platform/PlatformTest.cpp
index 8ab223ea2cd57..6299197791fc4 100644
--- a/lldb/unittests/Platform/PlatformTest.cpp
+++ b/lldb/unittests/Platform/PlatformTest.cpp
@@ -157,3 +157,9 @@ TEST_F(PlatformTest, GetPlatformForArchitecturesCandidates) {
 
   PlatformThumb::Terminate();
 }
+
+TEST_F(PlatformTest, CreateUnknown) {
+  SetHostPlatform(std::make_shared<PlatformIntel>());
+  ASSERT_EQ(list.Create("unknown-platform-name"), nullptr);
+  ASSERT_EQ(list.GetOrCreate("dummy"), nullptr);
+}

@igorkudrin igorkudrin merged commit f8a4f61 into llvm:main Aug 5, 2025
11 checks passed
@igorkudrin igorkudrin deleted the lldb-avoid-crash-unknown-platform branch August 5, 2025 19:42
krishna2803 pushed a commit to krishna2803/llvm-project that referenced this pull request Aug 12, 2025
`PlatformList::Create()` added an item to the list even when
`Platform::Create()` returned `nullptr`. Other methods use these items
without checking, which can lead to a crash. For example:

```
> lldb
(lldb) platform select unknown
error: unable to find a plug-in for the platform named "unknown"
(lldb) file a.out-arm64
PLEASE submit a bug report to...
Stack dump:
0.      Program arguments: lldb
1.      HandleCommand(command = "file a.out-arm64 ")
...
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants