[libcxx-commits] [libcxx] [libc++] Add assert test for string assign/append (PR #207164)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 2 05:22:55 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

Fixes https://github.com/llvm/llvm-project/pull/206320#discussion_r3507502638.

---
Full diff: https://github.com/llvm/llvm-project/pull/207164.diff


2 Files Affected:

- (added) libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.append.pass.cpp (+39) 
- (added) libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.assign.pass.cpp (+39) 


``````````diff
diff --git a/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.append.pass.cpp b/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.append.pass.cpp
new file mode 100644
index 0000000000000..bbf99ffa8d488
--- /dev/null
+++ b/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.append.pass.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// basic_string& append(const value_type* s, size_type n);
+// basic_string& append(const value_type* s);
+// basic_string& append(const value_type* s, size_type pos, size_type n);
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+#include <string>
+
+#include "check_assertion.h"
+#include "min_allocator.h"
+
+template <class S>
+void test() {
+  S l1("123");
+  const char* np = nullptr;
+  TEST_LIBCPP_ASSERT_FAILURE(l1.append(np, 1), "string::append received nullptr");
+  TEST_LIBCPP_ASSERT_FAILURE(l1.append(np), "string::append received nullptr");
+  TEST_LIBCPP_ASSERT_FAILURE(l1.append(np, 0, 1), "string::append received nullptr");
+}
+
+int main(int, char**) {
+  test<std::string>();
+  test<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
+
+  return 0;
+}
diff --git a/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.assign.pass.cpp b/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.assign.pass.cpp
new file mode 100644
index 0000000000000..d5a458a3bd0eb
--- /dev/null
+++ b/libcxx/test/libcxx/strings/basic.string/string.modifiers/assert.assign.pass.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// basic_string& assign(const value_type* s, size_type n);
+// basic_string& assign(const value_type* s);
+// basic_string& assign(const value_type* s, size_type pos, size_type n);
+
+// REQUIRES: has-unix-headers
+// UNSUPPORTED: c++03
+// UNSUPPORTED: libcpp-hardening-mode=none
+// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
+
+#include <string>
+
+#include "check_assertion.h"
+#include "min_allocator.h"
+
+template <class S>
+void test() {
+  S l1("123");
+  const char* np = nullptr;
+  TEST_LIBCPP_ASSERT_FAILURE(l1.assign(np, 1), "string::assign received nullptr");
+  TEST_LIBCPP_ASSERT_FAILURE(l1.assign(np), "string::assign received nullptr");
+  TEST_LIBCPP_ASSERT_FAILURE(l1.assign(np, 0, 1), "string::assign received nullptr");
+}
+
+int main(int, char**) {
+  test<std::string>();
+  test<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
+
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/207164


More information about the libcxx-commits mailing list