[llvm-branch-commits] [lldb] release/23.x: [lldb][API] Fix SBEnvironment crash when Set is called with a nullptr. (#218906) (PR #219500)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Aug 28 08:20:15 PDT 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/219500

Backport 69db8489be2f4e3cb1cf3f874403481d206b545c

Requested by: @da-viper

>From 268b4a20f6f11e0651221249d213986b3b3de60a Mon Sep 17 00:00:00 2001
From: Ebuka Ezike <e_ezike at apple.com>
Date: Fri, 28 Aug 2026 16:11:09 +0100
Subject: [PATCH] [lldb][API] Fix SBEnvironment crash when Set is called with a
 nullptr. (#218906)

Crashed because std::string is constructed with a nullptr. Wrap with
`llvm::StringRef`.

Add a unittest

(cherry picked from commit 69db8489be2f4e3cb1cf3f874403481d206b545c)
---
 lldb/source/API/SBEnvironment.cpp        | 15 ++++++++--
 lldb/unittests/API/CMakeLists.txt        |  1 +
 lldb/unittests/API/SBEnvironmentTest.cpp | 36 ++++++++++++++++++++++++
 3 files changed, 49 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..67676aec19dfc 100644
--- a/lldb/source/API/SBEnvironment.cpp
+++ b/lldb/source/API/SBEnvironment.cpp
@@ -75,17 +75,26 @@ 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};
+  if (name_ref.trim().empty())
+    return false;
+
+  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);
+  llvm::StringRef name_ref{name};
+  if (name_ref.trim().empty())
+    return false;
+
+  return m_opaque_up->erase(name_ref);
 }
 
 SBStringList SBEnvironment::GetEntries() {
diff --git a/lldb/unittests/API/CMakeLists.txt b/lldb/unittests/API/CMakeLists.txt
index b86054fb353f7..46ca6635d4305 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..f18711dcdb994
--- /dev/null
+++ b/lldb/unittests/API/SBEnvironmentTest.cpp
@@ -0,0 +1,36 @@
+//===-- 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
+//
+//===----------------------------------------------------------------------===/
+
+// Use the umbrella header for -Wdocumentation.
+#include "lldb/API/LLDB.h"
+
+#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.
+  EXPECT_TRUE(env.Set("FOO", nullptr, false));
+  const char *foo_val = env.Get("FOO");
+  EXPECT_STREQ(foo_val, "");
+
+  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 llvm-branch-commits mailing list