Skip to content

Commit 988876c

Browse files
authored
Fix wcpncpy() return value; add test.
1 parent dfc5987 commit 988876c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

libc/src/wchar/wcpncpy.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ LLVM_LIBC_FUNCTION(wchar_t *, wcpncpy,
2828
for (i = 0; i < n && s2[i] != '\0'; ++i)
2929
s1[i] = s2[i];
3030
// When n>strlen(src), n-strlen(src) \0 are appended.
31-
for (; i < n; ++i)
32-
s1[i] = L'\0';
31+
for (size_t j = i; j < n; ++j)
32+
s1[j] = L'\0';
33+
// ...but our result points to the first \0 (if any).
3334
return s1 + i;
3435
}
3536

libc/test/src/wchar/wcpncpy_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ TEST(LlvmLibcWCPNCpyTest, CopyTwoWithNull) {
7575
ASSERT_EQ(dest + 2, res);
7676
}
7777

78+
TEST(LlvmLibcWCPNCpyTest, CopyAndFill) {
79+
wchar_t dest[] = {L'a', L'b', L'c'};
80+
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, L"x", 3);
81+
ASSERT_TRUE(dest[0] == L'x');
82+
ASSERT_TRUE(dest[1] == L'\0');
83+
ASSERT_TRUE(dest[2] == L'\0');
84+
ASSERT_EQ(dest + 1, res);
85+
}
86+
7887
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)
7988
TEST(LlvmLibcWCPNCpyTest, NullptrCrash) {
8089
// Passing in a nullptr should crash the program.

0 commit comments

Comments
 (0)