https://github.com/Michael137 created https://github.com/llvm/llvm-project/pull/95175
This patch prevents passing a `nullptr` to the `std::string` constructor in `GetString`. This prevents UB arising from calling `GetString` on a default-constructed `ConstString`. >From adacdee08b3d4f13beffff32ae3223a8272c15f7 Mon Sep 17 00:00:00 2001 From: Michael Buch <michaelbuc...@gmail.com> Date: Tue, 11 Jun 2024 22:23:56 +0100 Subject: [PATCH] [lldb][ConstString] Prevent GetString from constructing a std::string with a nullptr This patch prevents passing a `nullptr` to the `std::string` constructor in `GetString`. This prevents UB arising from calling `GetString` on a default-constructed `ConstString`. --- lldb/include/lldb/Utility/ConstString.h | 4 +++- lldb/unittests/Utility/ConstStringTest.cpp | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lldb/include/lldb/Utility/ConstString.h b/lldb/include/lldb/Utility/ConstString.h index f7f7ec7605eba..692ecb63bd763 100644 --- a/lldb/include/lldb/Utility/ConstString.h +++ b/lldb/include/lldb/Utility/ConstString.h @@ -199,7 +199,9 @@ class ConstString { } /// Get the string value as a std::string - std::string GetString() const { return std::string(m_string, GetLength()); } + std::string GetString() const { + return std::string(AsCString(""), GetLength()); + } /// Get the string value as a C string. /// diff --git a/lldb/unittests/Utility/ConstStringTest.cpp b/lldb/unittests/Utility/ConstStringTest.cpp index 716f2d8d6c804..7018248991b6e 100644 --- a/lldb/unittests/Utility/ConstStringTest.cpp +++ b/lldb/unittests/Utility/ConstStringTest.cpp @@ -88,6 +88,7 @@ TEST(ConstStringTest, NullAndEmptyStates) { EXPECT_TRUE(!null); EXPECT_TRUE(null.IsEmpty()); EXPECT_TRUE(null.IsNull()); + EXPECT_TRUE(null.GetString().empty()); } TEST(ConstStringTest, CompareConstString) { _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits