Skip to content

Commit 860f0b5

Browse files
authored
[lldb][ConstString] Prevent GetString from constructing a std::string with a nullptr (#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`.
1 parent 1efd5c2 commit 860f0b5

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

lldb/include/lldb/Utility/ConstString.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ class ConstString {
199199
}
200200

201201
/// Get the string value as a std::string
202-
std::string GetString() const { return std::string(m_string, GetLength()); }
202+
std::string GetString() const {
203+
return std::string(AsCString(""), GetLength());
204+
}
203205

204206
/// Get the string value as a C string.
205207
///

lldb/unittests/Utility/ConstStringTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ TEST(ConstStringTest, NullAndEmptyStates) {
8888
EXPECT_TRUE(!null);
8989
EXPECT_TRUE(null.IsEmpty());
9090
EXPECT_TRUE(null.IsNull());
91+
EXPECT_TRUE(null.GetString().empty());
9192
}
9293

9394
TEST(ConstStringTest, CompareConstString) {

0 commit comments

Comments
 (0)