Skip to content

Commit e8a3794

Browse files
committed
added errno handling
1 parent d0cb767 commit e8a3794

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

libc/src/wchar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ add_entrypoint_object(
5858
libc.hdr.types.wchar_t
5959
libc.src.__support.wchar.wcrtomb
6060
libc.src.__support.wchar.mbstate
61+
libc.src.__support.libc_errno
6162
)
6263

6364
add_entrypoint_object(

libc/src/wchar/wctomb.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "hdr/types/wchar_t.h"
1212
#include "src/__support/common.h"
13+
#include "src/__support/libc_errno.h"
1314
#include "src/__support/macros/config.h"
1415
#include "src/__support/wchar/mbstate.h"
1516
#include "src/__support/wchar/wcrtomb.h"
@@ -23,8 +24,10 @@ LLVM_LIBC_FUNCTION(int, wctomb, (char *s, wchar_t wc)) {
2324

2425
auto result = internal::wcrtomb(s, wc, &internal_mbstate);
2526

26-
if (!result.has_value()) // invalid wide character
27+
if (!result.has_value()) { // invalid wide character
28+
libc_errno = EILSEQ;
2729
return -1;
30+
}
2831

2932
return static_cast<int>(result.value());
3033
}

libc/test/src/wchar/wctomb_test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "hdr/types/wchar_t.h"
10+
#include "src/__support/libc_errno.h"
1011
#include "src/wchar/wctomb.h"
12+
#include "test/UnitTest/ErrnoCheckingTest.h"
1113
#include "test/UnitTest/Test.h"
1214

15+
using LlvmLibcWCToMBTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
16+
1317
TEST(LlvmLibcWCToMBTest, OneByte) {
1418
wchar_t wc = L'U';
1519
char mb[4];
@@ -65,4 +69,5 @@ TEST(LlvmLibcWCToMBTest, InvalidWchar) {
6569
char mb[4];
6670
int cnt = LIBC_NAMESPACE::wctomb(mb, wc);
6771
ASSERT_EQ(cnt, -1);
72+
ASSERT_ERRNO_EQ(EILSEQ);
6873
}

0 commit comments

Comments
 (0)