Skip to content

Commit b078334

Browse files
committed
added errno checks + formatting
1 parent 04a67fd commit b078334

File tree

6 files changed

+22
-15
lines changed

6 files changed

+22
-15
lines changed

libc/src/__support/wchar/wcrtomb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps,
2626
static_assert(sizeof(wchar_t) == 4);
2727

2828
CharacterConverter cr(ps);
29-
29+
3030
if (!cr.isValidState())
3131
return Error(EINVAL);
32-
32+
3333
char buf[sizeof(wchar_t) / sizeof(char)];
3434
if (s == nullptr)
3535
s = buf;
36-
36+
3737
// if cr isnt empty, it should be represented in mbstate already
3838
if (cr.isEmpty()) {
3939
int status = cr.push(static_cast<char32_t>(wc));
4040
if (status != 0)
4141
return Error(EILSEQ);
4242
}
43-
43+
4444
size_t count = 0;
4545
while (!cr.isEmpty() && count < max_written) {
4646
auto utf8 = cr.pop_utf8(); // can never fail as long as the push succeeded

libc/src/__support/wchar/wcrtomb.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
namespace LIBC_NAMESPACE_DECL {
1919
namespace internal {
2020

21-
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps, size_t max_written);
21+
ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc, mbstate *__restrict ps,
22+
size_t max_written);
2223

2324
} // namespace internal
2425
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/wchar/wcsrtombs.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ namespace internal {
2222

2323
ErrorOr<size_t> wcsrtombs(char *__restrict dst, const wchar_t **__restrict src,
2424
size_t len, mbstate *__restrict ps) {
25-
if (src == nullptr)
26-
return Error(-1);
27-
2825
// ignore len parameter when theres no destination string
2926
if (dst == nullptr)
3027
len = SIZE_MAX;

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, sizeof (wchar_t));
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/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ add_libc_test(
8989
libc.src.string.memset
9090
libc.hdr.types.wchar_t
9191
libc.hdr.types.mbstate_t
92+
libc.src.__support.wchar.mbstate
9293
libc.src.__support.libc_errno
9394
)
9495

libc/test/src/wchar/wcsrtombs_test.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, SingleCharacterOneByte) {
2222
const wchar_t *wcs = L"U";
2323
char mbs[] = {0, 0};
2424
size_t cnt = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 2, &state);
25+
ASSERT_ERRNO_SUCCESS();
2526
ASSERT_EQ(cnt, static_cast<size_t>(1));
2627
ASSERT_EQ(mbs[0], 'U');
2728
ASSERT_EQ(mbs[1], '\0');
@@ -42,13 +43,15 @@ TEST_F(LlvmLibcWCSRToMBSTest, MultipleCompleteConversions) {
4243
char expected[6] = {'\xC3', '\xBF', '\xEA', '\xB0', '\x95', '\x00'};
4344

4445
size_t cnt1 = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 2, &state);
46+
ASSERT_ERRNO_SUCCESS();
4547
ASSERT_EQ(cnt1, static_cast<size_t>(2));
4648
ASSERT_EQ(wcs, wcs_start + 1);
4749
ASSERT_EQ(mbs[0], expected[0]);
4850
ASSERT_EQ(mbs[1], expected[1]);
4951
ASSERT_EQ(mbs[2], '\x01'); // not modified
5052

5153
size_t cnt2 = LIBC_NAMESPACE::wcsrtombs(mbs + cnt1, &wcs, 3, &state);
54+
ASSERT_ERRNO_SUCCESS();
5255
ASSERT_EQ(cnt2, static_cast<size_t>(3));
5356
ASSERT_EQ(wcs, wcs_start + 2);
5457
ASSERT_EQ(mbs[0], expected[0]);
@@ -60,6 +63,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, MultipleCompleteConversions) {
6063

6164
// all that is left in the string is the null terminator
6265
size_t cnt3 = LIBC_NAMESPACE::wcsrtombs(mbs + cnt1 + cnt2, &wcs, 50, &state);
66+
ASSERT_ERRNO_SUCCESS();
6367
ASSERT_EQ(cnt3, static_cast<size_t>(0));
6468
ASSERT_EQ(wcs, nullptr);
6569
ASSERT_EQ(mbs[0], expected[0]);
@@ -87,13 +91,15 @@ TEST_F(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
8791
size_t count = 0;
8892

8993
count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 1, &state);
94+
ASSERT_ERRNO_SUCCESS();
9095
written += count;
9196
// ASSERT_EQ(count, static_cast<size_t>(1));
9297
ASSERT_EQ(wcs, wcs_start);
9398
ASSERT_EQ(mbs[0], expected[0]);
9499
ASSERT_EQ(mbs[1], '\x01');
95100

96101
count = LIBC_NAMESPACE::wcsrtombs(mbs + written, &wcs, 2, &state);
102+
ASSERT_ERRNO_SUCCESS();
97103
written += count;
98104
ASSERT_EQ(count, static_cast<size_t>(2));
99105
ASSERT_EQ(wcs, wcs_start + 1);
@@ -103,6 +109,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, MultiplePartialConversions) {
103109
ASSERT_EQ(mbs[3], '\x01');
104110

105111
count = LIBC_NAMESPACE::wcsrtombs(mbs + written, &wcs, 3, &state);
112+
ASSERT_ERRNO_SUCCESS();
106113
written += count;
107114
ASSERT_EQ(count, static_cast<size_t>(2));
108115
ASSERT_EQ(wcs, nullptr);
@@ -126,6 +133,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, NullDestination) {
126133
// null destination means the conversion isnt stored, but all the side effects
127134
// still occur. the len parameter is also ignored
128135
size_t count = LIBC_NAMESPACE::wcsrtombs(nullptr, &wcs, 3, &state);
136+
ASSERT_ERRNO_SUCCESS();
129137
ASSERT_EQ(count, static_cast<size_t>(7));
130138
ASSERT_EQ(wcs, nullptr);
131139
}
@@ -144,13 +152,15 @@ TEST_F(LlvmLibcWCSRToMBSTest, NullState) {
144152
size_t count = 0;
145153

146154
count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 1, nullptr);
155+
ASSERT_ERRNO_SUCCESS();
147156
written += count;
148157
ASSERT_EQ(count, static_cast<size_t>(1));
149158
ASSERT_EQ(wcs, wcs_start);
150159
ASSERT_EQ(mbs[0], expected[0]);
151160
ASSERT_EQ(mbs[1], '\x01');
152161

153162
count = LIBC_NAMESPACE::wcsrtombs(mbs + written, &wcs, 2, nullptr);
163+
ASSERT_ERRNO_SUCCESS();
154164
written += count;
155165
ASSERT_EQ(count, static_cast<size_t>(2));
156166
ASSERT_EQ(wcs, wcs_start + 1);
@@ -160,6 +170,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, NullState) {
160170
ASSERT_EQ(mbs[3], '\x01');
161171

162172
count = LIBC_NAMESPACE::wcsrtombs(mbs + written, &wcs, 3, nullptr);
173+
ASSERT_ERRNO_SUCCESS();
163174
written += count;
164175
ASSERT_EQ(count, static_cast<size_t>(2));
165176
ASSERT_EQ(wcs, nullptr);
@@ -180,9 +191,9 @@ TEST_F(LlvmLibcWCSRToMBSTest, InvalidWchar) {
180191
char mbs[15];
181192
// convert the valid wchar
182193
size_t count = LIBC_NAMESPACE::wcsrtombs(mbs, &wcs, 5, &state);
194+
ASSERT_ERRNO_SUCCESS();
183195
ASSERT_EQ(count, static_cast<size_t>(5));
184196
ASSERT_TRUE(*wcs == static_cast<wchar_t>(0x12ffff));
185-
ASSERT_ERRNO_SUCCESS();
186197

187198
count = LIBC_NAMESPACE::wcsrtombs(mbs + count, &wcs, 5, &state); // invalid
188199
ASSERT_EQ(count, static_cast<size_t>(-1));
@@ -204,10 +215,7 @@ TEST_F(LlvmLibcWCSRToMBSTest, InvalidState) {
204215
TEST_F(LlvmLibcWCSRToMBSTest, NullSrc) {
205216
// Passing in a nullptr should crash the program.
206217
char mbs[] = {0, 0};
207-
EXPECT_DEATH(
208-
[&mbs] {
209-
LIBC_NAMESPACE::wcsrtombs(mbs, nullptr, 2, nullptr);
210-
},
211-
WITH_SIGNAL(-1));
218+
EXPECT_DEATH([&mbs] { LIBC_NAMESPACE::wcsrtombs(mbs, nullptr, 2, nullptr); },
219+
WITH_SIGNAL(-1));
212220
}
213221
#endif // LIBC_HAS_ADDRESS_SANITIZER

0 commit comments

Comments
 (0)