Skip to content

Commit fe88a13

Browse files
committed
add test for invalid state
1 parent 72a87b0 commit fe88a13

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

libc/src/wchar/wcrtomb.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ LLVM_LIBC_FUNCTION(size_t, wcrtomb,
2323
static internal::mbstate internal_mbstate;
2424

2525
// when s is nullptr, this is equivalent to wcrtomb(buf, L'\0', ps)
26-
if (s == nullptr)
26+
if (s == nullptr)
2727
wc = L'\0';
2828

2929
auto result = internal::wcrtomb(
3030
s, wc,
3131
ps == nullptr ? &internal_mbstate
32-
: reinterpret_cast<internal::mbstate *>(ps), 4);
32+
: reinterpret_cast<internal::mbstate *>(ps),
33+
sizeof(wchar_t));
3334

3435
if (!result.has_value()) {
3536
libc_errno = EILSEQ;

libc/src/wchar/wctomb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) {
2222
if (s == nullptr)
2323
return 0;
2424

25-
auto result = internal::wcrtomb(s, wc, &internal_mbstate);
25+
auto result = internal::wcrtomb(s, wc, &internal_mbstate, sizeof (wchar_t));
2626

2727
if (!result.has_value()) { // invalid wide character
2828
libc_errno = EILSEQ;

libc/test/src/wchar/wcsrtombs_test.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
#include "hdr/types/mbstate_t.h"
1010
#include "hdr/types/wchar_t.h"
11-
#include "src/__support/libc_errno.h"
1211
#include "src/string/memset.h"
1312
#include "src/wchar/wcsrtombs.h"
13+
#include "test/UnitTest/ErrnoCheckingTest.h"
1414
#include "test/UnitTest/Test.h"
15+
#include "src/__support/wchar/mbstate.h"
16+
17+
using LlvmLibcWCSRToMBSTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
1518

1619
TEST(LlvmLibcWCSRToMBSTest, SingleCharacterOneByte) {
1720
mbstate_t state;
@@ -85,7 +88,7 @@ TEST(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
8588

8689
count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 1, &state);
8790
written += count;
88-
//ASSERT_EQ(count, static_cast<size_t>(1));
91+
// ASSERT_EQ(count, static_cast<size_t>(1));
8992
ASSERT_EQ(wcs, wcs_start);
9093
ASSERT_EQ(mbs[0], expected[0]);
9194
ASSERT_EQ(mbs[1], '\x01');
@@ -179,8 +182,19 @@ TEST(LlvmLibcWCSRToMBSTest, InvalidWchar) {
179182
size_t count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 5, &state);
180183
ASSERT_EQ(count, static_cast<size_t>(5));
181184
ASSERT_TRUE(*wcs == static_cast<wchar_t>(0x12ffff));
185+
ASSERT_ERRNO_SUCCESS();
182186

183187
count = LIBC_NAMESPACE::wcsrtombs(mbs + count, &wcs, 5, &state); // invalid
184188
ASSERT_EQ(count, static_cast<size_t>(-1));
185-
ASSERT_EQ(static_cast<int>(libc_errno), EILSEQ);
189+
ASSERT_ERRNO_EQ(EILSEQ);
190+
}
191+
192+
TEST(LlvmLibcWCSRToMBSTest, InvalidState) {
193+
LIBC_NAMESPACE::internal::mbstate state{0, 0, 9}; // 9 total bytes is invalid
194+
const wchar_t *wcs = L"\xFF\xAC15";
195+
char mbs[5];
196+
// convert the valid wchar
197+
size_t count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 5, reinterpret_cast<mbstate_t*>(&state));
198+
ASSERT_EQ(count, static_cast<size_t>(-1));
199+
ASSERT_ERRNO_EQ(EINVAL);
186200
}

0 commit comments

Comments
 (0)