[Lldb-commits] [lldb] [lldb][API] Fix SBEnvironment crash when Set is called with a nullptr. (PR #218906)
Ebuka Ezike via lldb-commits
lldb-commits at lists.llvm.org
Wed Aug 26 06:21:41 PDT 2026
https://github.com/da-viper updated https://github.com/llvm/llvm-project/pull/218906
>From 3d6dcad3b34df2b0869329f0ac1f25f3e3e593c8 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <e_ezike at apple.com>
Date: Wed, 26 Aug 2026 12:47:01 +0100
Subject: [PATCH 1/2] [lldb][API] Fix SBEnvironment crash when Set is called
with a nullptr.
Crashed because std::string is constructed with a nullptr.
wrap with llvm::StringRef.
Add a unittest
---
lldb/source/API/SBEnvironment.cpp | 8 +++++---
lldb/unittests/API/CMakeLists.txt | 1 +
lldb/unittests/API/SBEnvironmentTest.cpp | 25 ++++++++++++++++++++++++
3 files changed, 31 insertions(+), 3 deletions(-)
create mode 100644 lldb/unittests/API/SBEnvironmentTest.cpp
diff --git a/lldb/source/API/SBEnvironment.cpp b/lldb/source/API/SBEnvironment.cpp
index 5fafabe02e014..e9678292589ab 100644
--- a/lldb/source/API/SBEnvironment.cpp
+++ b/lldb/source/API/SBEnvironment.cpp
@@ -75,17 +75,19 @@ const char *SBEnvironment::GetValueAtIndex(size_t index) {
bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
LLDB_INSTRUMENT_VA(this, name, value, overwrite);
+ llvm::StringRef name_ref{name};
+ llvm::StringRef value_ref{value};
if (overwrite) {
- m_opaque_up->insert_or_assign(name, std::string(value));
+ m_opaque_up->insert_or_assign(name_ref, value_ref.str());
return true;
}
- return m_opaque_up->try_emplace(name, std::string(value)).second;
+ return m_opaque_up->try_emplace(name_ref, value_ref.str()).second;
}
bool SBEnvironment::Unset(const char *name) {
LLDB_INSTRUMENT_VA(this, name);
- return m_opaque_up->erase(name);
+ return m_opaque_up->erase(llvm::StringRef(name));
}
SBStringList SBEnvironment::GetEntries() {
diff --git a/lldb/unittests/API/CMakeLists.txt b/lldb/unittests/API/CMakeLists.txt
index a7db51aea4994..be0f32b22ba5a 100644
--- a/lldb/unittests/API/CMakeLists.txt
+++ b/lldb/unittests/API/CMakeLists.txt
@@ -1,5 +1,6 @@
add_lldb_unittest(APITests
SBCommandInterpreterTest.cpp
+ SBEnvironmentTest.cpp
SBLineEntryTest.cpp
SBMutexTest.cpp
SBBreakpointClearConditionTest.cpp
diff --git a/lldb/unittests/API/SBEnvironmentTest.cpp b/lldb/unittests/API/SBEnvironmentTest.cpp
new file mode 100644
index 0000000000000..2e4d7b09a21a7
--- /dev/null
+++ b/lldb/unittests/API/SBEnvironmentTest.cpp
@@ -0,0 +1,25 @@
+//===-- SBEnvironment.cpp -------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===/
+
+#include "lldb/API/SBEnvironment.h"
+#include "gtest/gtest.h"
+
+TEST(SBEnvironmentTest, SetAndGetEnv) {
+
+ lldb::SBEnvironment env{};
+
+ // Setting an env var without a value does not crash.
+ env.Set("FOO", nullptr, false);
+ const char *foo_val = env.Get("FOO");
+ EXPECT_STREQ(foo_val, "");
+
+ env.Set("BAR", "BAR_VALUE", true);
+ env.Set("BAR", nullptr, true);
+ const char *bar_val = env.Get("BAR");
+ EXPECT_STREQ(bar_val, "") << "'BAR' should return the most recent value";
+}
>From 1d13af413b8d09d76e40ab5d8ac29bbb1aec24b3 Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <yerimyah1 at gmail.com>
Date: Wed, 26 Aug 2026 14:15:27 +0100
Subject: [PATCH 2/2] [lldb] Do not accept empty environment keys.
---
lldb/source/API/SBEnvironment.cpp | 7 +++++++
lldb/unittests/API/SBEnvironmentTest.cpp | 14 +++++++++++---
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/lldb/source/API/SBEnvironment.cpp b/lldb/source/API/SBEnvironment.cpp
index e9678292589ab..24add25329bbc 100644
--- a/lldb/source/API/SBEnvironment.cpp
+++ b/lldb/source/API/SBEnvironment.cpp
@@ -76,6 +76,9 @@ bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
LLDB_INSTRUMENT_VA(this, name, value, overwrite);
llvm::StringRef name_ref{name};
+ if (name_ref.trim().empty())
+ return false;
+
llvm::StringRef value_ref{value};
if (overwrite) {
m_opaque_up->insert_or_assign(name_ref, value_ref.str());
@@ -87,6 +90,10 @@ bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
bool SBEnvironment::Unset(const char *name) {
LLDB_INSTRUMENT_VA(this, name);
+ llvm::StringRef name_ref{name};
+ if (name_ref.trim().empty())
+ return false;
+
return m_opaque_up->erase(llvm::StringRef(name));
}
diff --git a/lldb/unittests/API/SBEnvironmentTest.cpp b/lldb/unittests/API/SBEnvironmentTest.cpp
index 2e4d7b09a21a7..84648488f697e 100644
--- a/lldb/unittests/API/SBEnvironmentTest.cpp
+++ b/lldb/unittests/API/SBEnvironmentTest.cpp
@@ -14,12 +14,20 @@ TEST(SBEnvironmentTest, SetAndGetEnv) {
lldb::SBEnvironment env{};
// Setting an env var without a value does not crash.
- env.Set("FOO", nullptr, false);
+ EXPECT_TRUE(env.Set("FOO", nullptr, false));
const char *foo_val = env.Get("FOO");
EXPECT_STREQ(foo_val, "");
- env.Set("BAR", "BAR_VALUE", true);
- env.Set("BAR", nullptr, true);
+ EXPECT_TRUE(env.Set("BAR", "BAR_VALUE", true));
+ EXPECT_TRUE(env.Set("BAR", nullptr, true));
const char *bar_val = env.Get("BAR");
EXPECT_STREQ(bar_val, "") << "'BAR' should return the most recent value";
+
+ EXPECT_FALSE(env.Set(nullptr, "VALUE", true));
+ EXPECT_FALSE(env.Set("", "VALUE", true));
+ EXPECT_FALSE(env.Set(" ", "VALUE", true));
+
+ EXPECT_FALSE(env.Get(nullptr));
+ EXPECT_FALSE(env.Get(""));
+ EXPECT_FALSE(env.Get(" "));
}
More information about the lldb-commits
mailing list