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

Roland McGrath via libc-commits libc-commits at lists.llvm.org
Sun Nov 29 13:01:21 PST 2026


https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/241063

>From 1d2d793720b7df6707fa54868b2f5a8b241d6476 Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Tue, 10 Nov 2026 15:08:33 -0800
Subject: [PATCH] [libc][test] Fix dangling temporary CString pointers in unit
 tests

libc_make_test_file_path returns a temporary CString wrapping an owning
cpp::string. Assigning its implicit const char * conversion to a raw
pointer member (in copy_file_range_test.cpp) or passing it 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
copy_file_range_test, getgrouplist_test, and initgroups_test.

Assisted-by: Gemini
---
 libc/src/__support/CMakeLists.txt             |  3 +
 libc/src/__support/c_string.h                 | 26 ++++++---
 libc/src/__support/macros/attributes.h        |  6 ++
 libc/test/src/__support/CMakeLists.txt        | 13 +++++
 libc/test/src/__support/c_string_test.cpp     | 56 +++++++++++++++++++
 libc/test/src/grp/getgrouplist_test.cpp       |  5 +-
 libc/test/src/grp/initgroups_test.cpp         |  4 +-
 libc/test/src/unistd/CMakeLists.txt           |  1 +
 libc/test/src/unistd/copy_file_range_test.cpp |  9 +--
 .../llvm-project-overlay/libc/BUILD.bazel     |  1 +
 .../libc/test/src/__support/BUILD.bazel       | 11 ++++
 11 files changed, 119 insertions(+), 16 deletions(-)
 create mode 100644 libc/test/src/__support/c_string_test.cpp

diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index a68f5e6b09d672..d01d343f0ddfd8 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 e4620f1c04d58f..9df3a95900df50 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -276,6 +276,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/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 3ce09ee110e0df..1ba50370862b3b 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -133,6 +133,7 @@ add_libc_test(
     libc.hdr.fcntl_macros
     libc.hdr.sys_stat_macros
     libc.src.__support.CPP.string_view
+    libc.src.__support.c_string
     libc.src.fcntl.open
     libc.src.unistd.close
     libc.src.unistd.copy_file_range
diff --git a/libc/test/src/unistd/copy_file_range_test.cpp b/libc/test/src/unistd/copy_file_range_test.cpp
index 67959d3d31d4f6..f572e1e970be9f 100644
--- a/libc/test/src/unistd/copy_file_range_test.cpp
+++ b/libc/test/src/unistd/copy_file_range_test.cpp
@@ -14,6 +14,7 @@
 #include "hdr/fcntl_macros.h"
 #include "hdr/sys_stat_macros.h"
 #include "src/__support/CPP/string_view.h"
+#include "src/__support/c_string.h"
 #include "src/fcntl/open.h"
 #include "src/unistd/close.h"
 #include "src/unistd/copy_file_range.h"
@@ -35,8 +36,8 @@ class LlvmLibcCopyFileRangeTest
   static constexpr const char TEST_DATA[] = "0123456789ABCDEF";
   static constexpr size_t TEST_SIZE = sizeof(TEST_DATA) - 1;
 
-  const char *in_path = nullptr;
-  const char *out_path = nullptr;
+  LIBC_NAMESPACE::CString in_path;
+  LIBC_NAMESPACE::CString out_path;
   int in_fd = -1;
   int out_fd = -1;
 
@@ -71,9 +72,9 @@ class LlvmLibcCopyFileRangeTest
       LIBC_NAMESPACE::close(out_fd);
       out_fd = -1;
     }
-    if (in_path)
+    if (in_path.c_str()[0] != '\0')
       LIBC_NAMESPACE::unlink(in_path);
-    if (out_path)
+    if (out_path.c_str()[0] != '\0')
       LIBC_NAMESPACE::unlink(out_path);
   }
 };
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 3deef74286e4a8..2ba7a5c896b5bc 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2160,6 +2160,7 @@ libc_support_library(
     deps = [
         ":__support_cpp_string",
         ":__support_cpp_string_view",
+        ":__support_cpp_utility",
         ":__support_macros_attributes",
         ":__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 a585fa1af9627f..708015b5f6c47f 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
@@ -106,6 +106,17 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "c_string_test",
+    srcs = ["c_string_test.cpp"],
+    deps = [
+        "//libc:__support_c_string",
+        "//libc:__support_cpp_string",
+        "//libc:__support_cpp_string_view",
+        "//libc:__support_cpp_utility",
+    ],
+)
+
 libc_test(
     name = "char_vector_test",
     srcs = ["char_vector_test.cpp"],



More information about the libc-commits mailing list