[Lldb-commits] [lldb] 860f0b5 - [lldb][ConstString] Prevent GetString from constructing a std::string with a nullptr (#95175)

via lldb-commits lldb-commits at lists.llvm.org
Wed Jun 12 01:34:15 PDT 2024


Author: Michael Buch
Date: 2024-06-12T09:34:12+01:00
New Revision: 860f0b542ae32c507959201146242cd716222041

URL: https://github.com/llvm/llvm-project/commit/860f0b542ae32c507959201146242cd716222041
DIFF: https://github.com/llvm/llvm-project/commit/860f0b542ae32c507959201146242cd716222041.diff

LOG: [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`.

Added: 
    

Modified: 
    lldb/include/lldb/Utility/ConstString.h
    lldb/unittests/Utility/ConstStringTest.cpp

Removed: 
    


################################################################################
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) {


        


More information about the lldb-commits mailing list