Skip to content

Commit e5e4455

Browse files
author
Sriya Pratipati
committed
fixed behavior for case with no null terminator
1 parent 21f31c6 commit e5e4455

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

libc/src/wchar/wcsnlen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
namespace LIBC_NAMESPACE_DECL {
1818

1919
LLVM_LIBC_FUNCTION(size_t, wcsnlen, (const wchar_t *src, size_t maxlen)) {
20-
size_t temp = internal::string_length(src);
21-
return temp > maxlen ? maxlen : temp;
20+
size_t i = 0;
21+
for (; i < maxlen && src[i]; ++i)
22+
;
23+
return i;
2224
}
2325

2426
} // namespace LIBC_NAMESPACE_DECL

libc/test/src/wchar/wcsnlen_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ TEST(LlvmLibcWCSNLenTest, IgnoreCharactersAfterNullTerminator) {
4444
ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::wcsnlen(src, 4));
4545
ASSERT_EQ(static_cast<size_t>(3), LIBC_NAMESPACE::wcsnlen(src, 5));
4646
}
47+
48+
TEST(LlvmLibcWCSNLenTest, NoNullTerminator) {
49+
const wchar_t src[4] = {L'a', L'b', L'c', L'd'};
50+
// Should return 4
51+
ASSERT_EQ(static_cast<size_t>(4), LIBC_NAMESPACE::wcsnlen(src, 4));
52+
// Should return 2 since N is smaller than string length
53+
ASSERT_EQ(static_cast<size_t>(2), LIBC_NAMESPACE::wcsnlen(src, 2));
54+
}

0 commit comments

Comments
 (0)