Skip to content

Commit f58caed

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Changed CharacterConverter returns (#146130)
changed internal CharacterConverter returns to return errno macro when necessary for consistency. --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 171aa34 commit f58caed

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

libc/src/__support/wchar/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_object_library(
1313
SRCS
1414
character_converter.cpp
1515
DEPENDS
16+
libc.hdr.errno_macros
1617
libc.hdr.types.char8_t
1718
libc.hdr.types.char32_t
1819
libc.src.__support.error_or

libc/src/__support/wchar/character_converter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "hdr/errno_macros.h"
910
#include "hdr/types/char32_t.h"
1011
#include "hdr/types/char8_t.h"
1112
#include "src/__support/CPP/bit.h"
@@ -76,7 +77,7 @@ int CharacterConverter::push(char8_t utf8_byte) {
7677
else {
7778
// bytes_stored and total_bytes will always be 0 here
7879
state->partial = static_cast<char32_t>(0);
79-
return -1;
80+
return EILSEQ;
8081
}
8182
state->partial = static_cast<char32_t>(utf8_byte);
8283
state->bytes_stored++;
@@ -93,7 +94,7 @@ int CharacterConverter::push(char8_t utf8_byte) {
9394
}
9495
// Invalid byte -> reset the state
9596
clear();
96-
return -1;
97+
return EILSEQ;
9798
}
9899

99100
int CharacterConverter::push(char32_t utf32) {
@@ -115,7 +116,7 @@ int CharacterConverter::push(char32_t utf32) {
115116
// `utf32` contains a value that is too large to actually represent a valid
116117
// unicode character
117118
clear();
118-
return -1;
119+
return EILSEQ;
119120
}
120121

121122
ErrorOr<char32_t> CharacterConverter::pop_utf32() {

libc/src/__support/wchar/mbrtowc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ ErrorOr<size_t> mbrtowc(wchar_t *__restrict pwc, const char *__restrict s,
3232
for (; i < n && !char_conv.isFull(); ++i) {
3333
int err = char_conv.push(static_cast<char8_t>(s[i]));
3434
// Encoding error
35-
if (err == -1)
36-
return Error(EILSEQ);
35+
if (err == EILSEQ)
36+
return Error(err);
3737
}
3838
auto wc = char_conv.pop_utf32();
3939
if (wc.has_value()) {

libc/src/__support/wchar/wcrtomb.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ ErrorOr<size_t> wcrtomb(char *__restrict s, wchar_t wc,
3030
if (!cr.isValidState())
3131
return Error(EINVAL);
3232

33-
if (s == nullptr)
34-
return Error(EILSEQ);
35-
3633
int status = cr.push(static_cast<char32_t>(wc));
3734
if (status != 0)
38-
return Error(EILSEQ);
35+
return Error(status);
3936

4037
size_t count = 0;
4138
while (!cr.isEmpty()) {

libc/test/src/__support/wchar/utf8_to_32_test.cpp

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

9+
#include "hdr/errno_macros.h"
910
#include "src/__support/error_or.h"
1011
#include "src/__support/wchar/character_converter.h"
1112
#include "src/__support/wchar/mbstate.h"
@@ -87,7 +88,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidByte) {
8788
LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
8889
int err = char_conv.push(static_cast<char8_t>(ch));
8990

90-
ASSERT_EQ(err, -1);
91+
ASSERT_EQ(err, EILSEQ);
9192
}
9293

9394
TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) {
@@ -100,12 +101,12 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) {
100101

101102
LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state);
102103
int err = char_conv.push(static_cast<char8_t>(ch[0]));
103-
ASSERT_EQ(err, -1);
104+
ASSERT_EQ(err, EILSEQ);
104105
err = char_conv.push(static_cast<char8_t>(ch[1]));
105106
ASSERT_EQ(err, 0);
106107
// Prev byte was single byte so trying to push another should error.
107108
err = char_conv.push(static_cast<char8_t>(ch[2]));
108-
ASSERT_EQ(err, -1);
109+
ASSERT_EQ(err, EILSEQ);
109110
err = char_conv.push(static_cast<char8_t>(ch[3]));
110111
ASSERT_EQ(err, 0);
111112
}
@@ -127,7 +128,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidLastByte) {
127128
err = char_conv.push(static_cast<char8_t>(ch[2]));
128129
ASSERT_EQ(err, 0);
129130
err = char_conv.push(static_cast<char8_t>(ch[3]));
130-
ASSERT_EQ(err, -1);
131+
ASSERT_EQ(err, EILSEQ);
131132
}
132133

133134
TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) {
@@ -144,7 +145,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) {
144145
ASSERT_EQ(err, 0);
145146
// Should produce an error on 3rd byte
146147
err = char_conv.push(static_cast<char8_t>(ch[2]));
147-
ASSERT_EQ(err, -1);
148+
ASSERT_EQ(err, EILSEQ);
148149

149150
// Should produce an error since mbstate was reset
150151
auto wch = char_conv.pop_utf32();

0 commit comments

Comments
 (0)