[libc-commits] [libc] 0111906 - [libc][test] Fix dangling temporary CString pointers in unit tests (#241063)

via libc-commits libc-commits at lists.llvm.org
Sun Nov 29 13:45:11 PST 2026


Author: Roland McGrath
Date: 2026-11-29T13:45:05-08:00
New Revision: 0111906503fd4b37fc568f798fa900a5f716d759

URL: https://github.com/llvm/llvm-project/commit/0111906503fd4b37fc568f798fa900a5f716d759
DIFF: https://github.com/llvm/llvm-project/commit/0111906503fd4b37fc568f798fa900a5f716d759.diff

LOG: [libc][test] Fix dangling temporary CString pointers in unit tests (#241063)

libc_make_test_file_path returns a temporary CString wrapping an owning
cpp::string. Passing its implicit const char * conversion directly to
TESTONLY_set_group_path (which stores the raw pointer without copying in
getgrouplist_test.cpp and initgroups_test.cpp) leaves a dangling heap
pointer once the full-expression ends whenever the filename exceeds
cpp::string's short-string buffer.

Annotate CString's pointer and string_view accessors with
LIBC_LIFETIMEBOUND so Clang diagnoses temporary lifetime escapes at
compile time, allow default and move construction/assignment on CString
for test fixture members, and keep the CString objects alive in
getgrouplist_test and initgroups_test.

Assisted-by: Gemini

Added: 
    libc/test/src/__support/c_string_test.cpp

Modified: 
    libc/src/__support/CMakeLists.txt
    libc/src/__support/c_string.h
    libc/src/__support/macros/attributes.h
    libc/test/src/__support/CMakeLists.txt
    libc/test/src/grp/getgrouplist_test.cpp
    libc/test/src/grp/initgroups_test.cpp
    utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
    utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 383c212e134923..2071b83e11940b 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -162,6 +162,9 @@ add_header_library(
     libc.src.__support.common
     libc.src.__support.CPP.string
     libc.src.__support.CPP.string_view
+    libc.src.__support.CPP.utility
+    libc.src.__support.macros.attributes
+    libc.src.__support.macros.config
 )
 
 add_header_library(

diff  --git a/libc/src/__support/c_string.h b/libc/src/__support/c_string.h
index 1e7d5558f9001e..f8fb0ef5f59250 100644
--- a/libc/src/__support/c_string.h
+++ b/libc/src/__support/c_string.h
@@ -11,6 +11,7 @@
 
 #include "src/__support/CPP/string.h"
 #include "src/__support/CPP/string_view.h"
+#include "src/__support/CPP/utility.h"
 #include "src/__support/macros/attributes.h" // for LIBC_INLINE
 #include "src/__support/macros/config.h"
 
@@ -23,15 +24,24 @@ class CString {
   cpp::string str;
 
 public:
-  // These constructors can be implemented iff required.
-  CString() = delete;
+  LIBC_INLINE CString() = default;
   CString(const CString &) = delete;
-  CString(CString &&) = delete;
-
-  LIBC_INLINE CString(cpp::string in_str) : str(in_str) {}
-
-  LIBC_INLINE operator const char *() const { return str.c_str(); }
-  LIBC_INLINE operator cpp::string_view() const { return str; }
+  CString &operator=(const CString &) = delete;
+  LIBC_INLINE CString(CString &&) = default;
+  LIBC_INLINE CString &operator=(CString &&) = default;
+
+  LIBC_INLINE CString(const char *cstr) : str(cstr) {}
+  LIBC_INLINE CString(cpp::string in_str) : str(cpp::move(in_str)) {}
+
+  LIBC_INLINE const char *c_str() const LIBC_LIFETIMEBOUND {
+    return str.c_str();
+  }
+  LIBC_INLINE operator const char *() const LIBC_LIFETIMEBOUND {
+    return str.c_str();
+  }
+  LIBC_INLINE operator cpp::string_view() const LIBC_LIFETIMEBOUND {
+    return str;
+  }
 };
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/__support/macros/attributes.h b/libc/src/__support/macros/attributes.h
index 6e6e359ae4c1eb..b155046e3d0d2b 100644
--- a/libc/src/__support/macros/attributes.h
+++ b/libc/src/__support/macros/attributes.h
@@ -198,6 +198,12 @@ LIBC_THREAD_MODE_EXTERNAL.
 #define LIBC_NO_UNIQUE_ADDRESS
 #endif
 
+#if __has_cpp_attribute(clang::lifetimebound)
+#define LIBC_LIFETIMEBOUND [[clang::lifetimebound]]
+#else
+#define LIBC_LIFETIMEBOUND
+#endif
+
 #if __has_attribute(trivial_abi)
 #define LIBC_TRIVIAL_ABI [[clang::trivial_abi]]
 #else

diff  --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index 112a2fcb1b6b6c..17eb20ae225358 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -282,6 +282,19 @@ add_libc_test(
     libc.src.__support.fixedvector
 )
 
+add_libc_test(
+  c_string_test
+  SUITE
+    libc-support-tests
+  SRCS
+    c_string_test.cpp
+  DEPENDS
+    libc.src.__support.c_string
+    libc.src.__support.CPP.string
+    libc.src.__support.CPP.string_view
+    libc.src.__support.CPP.utility
+)
+
 add_libc_test(
   char_vector_test
   SUITE

diff  --git a/libc/test/src/__support/c_string_test.cpp b/libc/test/src/__support/c_string_test.cpp
new file mode 100644
index 00000000000000..699a2898c947ed
--- /dev/null
+++ b/libc/test/src/__support/c_string_test.cpp
@@ -0,0 +1,56 @@
+//===-- Unittests for CString ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unit tests for CString.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/string.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/CPP/utility.h"
+#include "src/__support/c_string.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::CString;
+using LIBC_NAMESPACE::cpp::string;
+using LIBC_NAMESPACE::cpp::string_view;
+
+TEST(LlvmLibcCStringTest, DefaultConstructedEmpty) {
+  const CString empty;
+  EXPECT_STREQ(empty.c_str(), "");
+  EXPECT_STREQ(static_cast<const char *>(empty), "");
+  EXPECT_EQ(string_view(empty), string_view(""));
+}
+
+TEST(LlvmLibcCStringTest, ConstructFromShortAndLongStrings) {
+  const CString from_cstr("short_path");
+  EXPECT_STREQ(from_cstr.c_str(), "short_path");
+  EXPECT_EQ(string_view(from_cstr), string_view("short_path"));
+
+  static constexpr char LONG_PATH[] =
+      "/tmp/llvm_libc_very_long_directory_prefix_exceeding_sso/test_file.dat";
+  const CString from_string{string(LONG_PATH)};
+  EXPECT_STREQ(from_string.c_str(), LONG_PATH);
+  EXPECT_STREQ(static_cast<const char *>(from_string), LONG_PATH);
+  EXPECT_EQ(string_view(from_string), string_view(LONG_PATH));
+}
+
+TEST(LlvmLibcCStringTest, MoveConstructAndAssign) {
+  static constexpr char LONG_PATH[] =
+      "/tmp/llvm_libc_move_semantics_heap_allocated_test_path/test_file.dat";
+
+  CString original{string(LONG_PATH)};
+  CString move_constructed(LIBC_NAMESPACE::cpp::move(original));
+  EXPECT_STREQ(move_constructed.c_str(), LONG_PATH);
+
+  CString move_assigned("initial");
+  move_assigned = LIBC_NAMESPACE::cpp::move(move_constructed);
+  EXPECT_STREQ(move_assigned.c_str(), LONG_PATH);
+  EXPECT_EQ(string_view(move_assigned), string_view(LONG_PATH));
+}

diff  --git a/libc/test/src/grp/getgrouplist_test.cpp b/libc/test/src/grp/getgrouplist_test.cpp
index d49aedb1adc10a..9e34ca9ab5e60e 100644
--- a/libc/test/src/grp/getgrouplist_test.cpp
+++ b/libc/test/src/grp/getgrouplist_test.cpp
@@ -128,8 +128,9 @@ TEST_F(LlvmLibcGrpTest, UserNotFound) {
 }
 
 TEST_F(LlvmLibcGrpTest, NonexistentFile) {
-  LIBC_NAMESPACE::grp::TESTONLY_set_group_path(
-      libc_make_test_file_path("getgrouplist_missing.test"));
+  const auto missing_path =
+      libc_make_test_file_path("getgrouplist_missing.test");
+  LIBC_NAMESPACE::grp::TESTONLY_set_group_path(missing_path);
 
   gid_t groups[5];
   int ngroups = 5;

diff  --git a/libc/test/src/grp/initgroups_test.cpp b/libc/test/src/grp/initgroups_test.cpp
index a024cab96fc057..49c442584f1f46 100644
--- a/libc/test/src/grp/initgroups_test.cpp
+++ b/libc/test/src/grp/initgroups_test.cpp
@@ -56,8 +56,8 @@ TEST_F(LlvmLibcGrpTest, InitgroupsNonexistentUser) {
 }
 
 TEST_F(LlvmLibcGrpTest, InitgroupsNonexistentFile) {
-  LIBC_NAMESPACE::grp::TESTONLY_set_group_path(
-      libc_make_test_file_path("initgroups_missing.test"));
+  const auto missing_path = libc_make_test_file_path("initgroups_missing.test");
+  LIBC_NAMESPACE::grp::TESTONLY_set_group_path(missing_path);
 
   if (LIBC_NAMESPACE::getuid() == 0) {
     EXPECT_THAT(LIBC_NAMESPACE::initgroups("anyuser", 1000), Succeeds(0));

diff  --git a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
index 0ad1b9432688f4..8c235412c2170d 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/BUILD.bazel
@@ -64,6 +64,7 @@ libc_support_library(
     deps = [
         "//libc/src/__support/CPP:string",
         "//libc/src/__support/CPP:string_view",
+        "//libc/src/__support/CPP:utility",
         "//libc/src/__support/macros:attributes",
         "//libc/src/__support/macros:config",
     ],

diff  --git a/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
index d3c470829d262b..bcfd36cf671240 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/__support/BUILD.bazel
@@ -109,6 +109,17 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "c_string_test",
+    srcs = ["c_string_test.cpp"],
+    deps = [
+        "//libc/src/__support:c_string",
+        "//libc/src/__support/CPP:string",
+        "//libc/src/__support/CPP:string_view",
+        "//libc/src/__support/CPP:utility",
+    ],
+)
+
 libc_test(
     name = "char_vector_test",
     srcs = ["char_vector_test.cpp"],


        


More information about the libc-commits mailing list