[Lldb-commits] [lldb] [lldb][ConstString] Prevent GetString from constructing a std::string with a nullptr (PR #95175)
via lldb-commits
lldb-commits at lists.llvm.org
Tue Jun 11 14:32:22 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-lldb
Author: Michael Buch (Michael137)
<details>
<summary>Changes</summary>
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`.
---
Full diff: https://github.com/llvm/llvm-project/pull/95175.diff
2 Files Affected:
- (modified) lldb/include/lldb/Utility/ConstString.h (+3-1)
- (modified) lldb/unittests/Utility/ConstStringTest.cpp (+1)
``````````diff
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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/95175
More information about the lldb-commits
mailing list