Reland "Fix wcpncpy() return value; add test." (#146753)

Reverts llvm/llvm-project#146752, which was a revert of my accidental
push, so we can actually review and presubmit this time.
This commit is contained in:
enh-google 2025-07-09 15:25:49 -04:00 committed by GitHub
parent 2366573679
commit ff365ce1d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 5 deletions

View File

@ -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;
}

View File

@ -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,16 @@ 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) {
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)