Fix wcpncpy() return value; add test.
diff --git a/libc/src/wchar/wcpncpy.cpp b/libc/src/wchar/wcpncpy.cpp
index 9f451b7..0e729db 100644
--- a/libc/src/wchar/wcpncpy.cpp
+++ b/libc/src/wchar/wcpncpy.cpp
@@ -28,8 +28,9 @@
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 98738e2..5ce3e8c 100644
--- a/libc/test/src/wchar/wcpncpy_test.cpp
+++ b/libc/test/src/wchar/wcpncpy_test.cpp
@@ -75,6 +75,15 @@
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.