Skip to content

Commit c44778c

Browse files
igorkudrinkrishna2803
authored andcommitted
[lldb] Avoid a crash after selecting an unknown platform (llvm#151803)
`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 ") ... ```
1 parent 713ea33 commit c44778c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lldb/source/Target/Platform.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,8 @@ PlatformSP PlatformList::GetOrCreate(llvm::ArrayRef<ArchSpec> archs,
22342234
PlatformSP PlatformList::Create(llvm::StringRef name) {
22352235
std::lock_guard<std::recursive_mutex> guard(m_mutex);
22362236
PlatformSP platform_sp = Platform::Create(name);
2237-
m_platforms.push_back(platform_sp);
2237+
if (platform_sp)
2238+
m_platforms.push_back(platform_sp);
22382239
return platform_sp;
22392240
}
22402241

lldb/unittests/Platform/PlatformTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,9 @@ TEST_F(PlatformTest, GetPlatformForArchitecturesCandidates) {
157157

158158
PlatformThumb::Terminate();
159159
}
160+
161+
TEST_F(PlatformTest, CreateUnknown) {
162+
SetHostPlatform(std::make_shared<PlatformIntel>());
163+
ASSERT_EQ(list.Create("unknown-platform-name"), nullptr);
164+
ASSERT_EQ(list.GetOrCreate("dummy"), nullptr);
165+
}

0 commit comments

Comments
 (0)