Skip to content

Commit ff365ce

Browse files
authored
Reland "Fix wcpncpy() return value; add test." (#146753)
Reverts #146752, which was a revert of my accidental push, so we can actually review and presubmit this time.
1 parent 2366573 commit ff365ce

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ TEST(LlvmLibcWCPNCpyTest, CopyNull) {
4545
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 1);
4646
ASSERT_TRUE(dest[0] == L'\0');
4747
ASSERT_TRUE(dest[1] == L'b');
48-
ASSERT_EQ(dest + 1, res);
48+
ASSERT_EQ(dest, res);
4949
}
5050

5151
TEST(LlvmLibcWCPNCpyTest, CopyPastSrc) {
@@ -54,7 +54,7 @@ TEST(LlvmLibcWCPNCpyTest, CopyPastSrc) {
5454
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 2);
5555
ASSERT_TRUE(dest[0] == L'\0');
5656
ASSERT_TRUE(dest[1] == L'\0');
57-
ASSERT_EQ(dest + 2, res);
57+
ASSERT_EQ(dest, res);
5858
}
5959

6060
TEST(LlvmLibcWCPNCpyTest, CopyTwoNoNull) {
@@ -72,7 +72,16 @@ TEST(LlvmLibcWCPNCpyTest, CopyTwoWithNull) {
7272
wchar_t *res = LIBC_NAMESPACE::wcpncpy(dest, src, 2);
7373
ASSERT_TRUE(dest[0] == L'x');
7474
ASSERT_TRUE(dest[1] == L'\0');
75-
ASSERT_EQ(dest + 2, res);
75+
ASSERT_EQ(dest + 1, res);
76+
}
77+
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);
7685
}
7786

7887
#if defined(LIBC_ADD_NULL_CHECKS) && !defined(LIBC_HAS_SANITIZER)

0 commit comments

Comments
 (0)