[libc-commits] [libc] Reland "Fix wcpncpy() return value; add test." (PR #146753)
via libc-commits
libc-commits at lists.llvm.org
Tue Jul 8 12:04:14 PDT 2025
https://github.com/enh-google updated https://github.com/llvm/llvm-project/pull/146753
>From b1bb298246aa96f413625bf2fccdf11d1b329ae7 Mon Sep 17 00:00:00 2001
From: enh-google <enh at google.com>
Date: Wed, 2 Jul 2025 14:13:00 -0400
Subject: [PATCH 1/2] Revert "Revert "Fix wcpncpy() return value; add test."
(#146752)"
This reverts commit 77d95911a3a4dc7445280cd5ee217e2a47a41b47.
---
libc/src/wchar/wcpncpy.cpp | 5 +++--
libc/test/src/wchar/wcpncpy_test.cpp | 9 +++++++++
2 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/libc/src/wchar/wcpncpy.cpp b/libc/src/wchar/wcpncpy.cpp
index 9f451b73f07cb..0e729db7abf60 100644
--- a/libc/src/wchar/wcpncpy.cpp
+++ b/libc/src/wchar/wcpncpy.cpp
@@ -28,8 +28,9 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcpncpy,
for (i = 0; i < n && s2[i] != '\0'; ++i)
s1[i] = s2[i];
// When n>strlen(src), n-strlen(src) \0 are appended.
- for (; i < n; ++i)
- s1[i] = L'\0';
+ for (size_t j = i; j < n; ++j)
+ s1[j] = L'\0';
+ // ...but our result points to the first \0 (if any).
return s1 + i;
}
diff --git a/libc/test/src/wchar/wcpncpy_test.cpp b/libc/test/src/wchar/wcpncpy_test.cpp
index 98738e230e32d..5ce3e8ce7cd93 100644
--- a/libc/test/src/wchar/wcpncpy_test.cpp
+++ b/libc/test/src/wchar/wcpncpy_test.cpp
@@ -75,6 +75,15 @@ TEST(LlvmLibcWCPNCpyTest, CopyTwoWithNull) {
ASSERT_EQ(dest + 2, res);
}
+TEST(LlvmLibcWCPNCpyTest, CopyAndFill) {
+ wchar_t dest[] = {L'a', L'b', L'c'};
+ wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, L"x", 3);
+ ASSERT_TRUE(dest[0] == L'x');
+ ASSERT_TRUE(dest[1] == L'\0');
+ ASSERT_TRUE(dest[2] == L'\0');
+ ASSERT_EQ(dest + 1, res);
+}
+
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
TEST(LlvmLibcWCPNCpyTest, NullptrCrash) {
// Passing in a nullptr should crash the program.
>From 423d4220dbc69236fc4c8a98ac75c7780eaece2c Mon Sep 17 00:00:00 2001
From: enh-google <enh at google.com>
Date: Tue, 8 Jul 2025 15:04:06 -0400
Subject: [PATCH 2/2] Update wcpncpy_test.cpp
---
libc/test/src/wchar/wcpncpy_test.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/libc/test/src/wchar/wcpncpy_test.cpp b/libc/test/src/wchar/wcpncpy_test.cpp
index 5ce3e8ce7cd93..bb72211f6264c 100644
--- a/libc/test/src/wchar/wcpncpy_test.cpp
+++ b/libc/test/src/wchar/wcpncpy_test.cpp
@@ -45,7 +45,7 @@ TEST(LlvmLibcWCPNCpyTest, CopyNull) {
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 1);
ASSERT_TRUE(dest[0] == L'\0');
ASSERT_TRUE(dest[1] == L'b');
- ASSERT_EQ(dest + 1, res);
+ ASSERT_EQ(dest, res);
}
TEST(LlvmLibcWCPNCpyTest, CopyPastSrc) {
@@ -54,7 +54,7 @@ TEST(LlvmLibcWCPNCpyTest, CopyPastSrc) {
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 2);
ASSERT_TRUE(dest[0] == L'\0');
ASSERT_TRUE(dest[1] == L'\0');
- ASSERT_EQ(dest + 2, res);
+ ASSERT_EQ(dest, res);
}
TEST(LlvmLibcWCPNCpyTest, CopyTwoNoNull) {
@@ -72,7 +72,7 @@ TEST(LlvmLibcWCPNCpyTest, CopyTwoWithNull) {
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 2);
ASSERT_TRUE(dest[0] == L'x');
ASSERT_TRUE(dest[1] == L'\0');
- ASSERT_EQ(dest + 2, res);
+ ASSERT_EQ(dest + 1, res);
}
TEST(LlvmLibcWCPNCpyTest, CopyAndFill) {
More information about the libc-commits
mailing list