From 3e647eb794c57d6029cbf9a81d72b76f61accb79 Mon Sep 17 00:00:00 2001 From: Sriya Pratipati Date: Fri, 27 Jun 2025 18:15:33 +0000 Subject: [PATCH 1/2] [libc] Changed CharacterConverter returns changed internal CharacterConverter returns to return errno macro when necessary for consistency. --- libc/src/__support/wchar/CMakeLists.txt | 1 + libc/src/__support/wchar/character_converter.cpp | 7 ++++--- libc/src/__support/wchar/mbrtowc.cpp | 4 ++-- libc/src/__support/wchar/wcrtomb.cpp | 5 +---- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/libc/src/__support/wchar/CMakeLists.txt b/libc/src/__support/wchar/CMakeLists.txt index 86a47319f278a..d3fb58ed0c71c 100644 --- a/libc/src/__support/wchar/CMakeLists.txt +++ b/libc/src/__support/wchar/CMakeLists.txt @@ -13,6 +13,7 @@ add_object_library( SRCS character_converter.cpp DEPENDS + libc.hdr.errno_macros libc.hdr.types.char8_t libc.hdr.types.char32_t libc.src.__support.error_or diff --git a/libc/src/__support/wchar/character_converter.cpp b/libc/src/__support/wchar/character_converter.cpp index c54a1b751f402..3cacfa5689e4d 100644 --- a/libc/src/__support/wchar/character_converter.cpp +++ b/libc/src/__support/wchar/character_converter.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "hdr/errno_macros.h" #include "hdr/types/char32_t.h" #include "hdr/types/char8_t.h" #include "src/__support/CPP/bit.h" @@ -76,7 +77,7 @@ int CharacterConverter::push(char8_t utf8_byte) { else { // bytes_stored and total_bytes will always be 0 here state->partial = static_cast(0); - return -1; + return EILSEQ; } state->partial = static_cast(utf8_byte); state->bytes_stored++; @@ -93,7 +94,7 @@ int CharacterConverter::push(char8_t utf8_byte) { } // Invalid byte -> reset the state clear(); - return -1; + return EILSEQ; } int CharacterConverter::push(char32_t utf32) { @@ -115,7 +116,7 @@ int CharacterConverter::push(char32_t utf32) { // `utf32` contains a value that is too large to actually represent a valid // unicode character clear(); - return -1; + return EILSEQ; } ErrorOr CharacterConverter::pop_utf32() { diff --git a/libc/src/__support/wchar/mbrtowc.cpp b/libc/src/__support/wchar/mbrtowc.cpp index 3b8f7666026c3..90ba934c42b69 100644 --- a/libc/src/__support/wchar/mbrtowc.cpp +++ b/libc/src/__support/wchar/mbrtowc.cpp @@ -32,8 +32,8 @@ ErrorOr mbrtowc(wchar_t *__restrict pwc, const char *__restrict s, for (; i < n && !char_conv.isFull(); ++i) { int err = char_conv.push(static_cast(s[i])); // Encoding error - if (err == -1) - return Error(EILSEQ); + if (err == EILSEQ) + return Error(err); } auto wc = char_conv.pop_utf32(); if (wc.has_value()) { diff --git a/libc/src/__support/wchar/wcrtomb.cpp b/libc/src/__support/wchar/wcrtomb.cpp index a74a6f3ec34a6..fc54bbfc93e0c 100644 --- a/libc/src/__support/wchar/wcrtomb.cpp +++ b/libc/src/__support/wchar/wcrtomb.cpp @@ -30,12 +30,9 @@ ErrorOr wcrtomb(char *__restrict s, wchar_t wc, if (!cr.isValidState()) return Error(EINVAL); - if (s == nullptr) - return Error(EILSEQ); - int status = cr.push(static_cast(wc)); if (status != 0) - return Error(EILSEQ); + return Error(status); size_t count = 0; while (!cr.isEmpty()) { From 3e6fa7fc1699f9fc3a04eb2d2649227e6da0460d Mon Sep 17 00:00:00 2001 From: Sriya Pratipati Date: Fri, 27 Jun 2025 20:11:18 +0000 Subject: [PATCH 2/2] updated test file with correct error values --- libc/test/src/__support/wchar/utf8_to_32_test.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/libc/test/src/__support/wchar/utf8_to_32_test.cpp b/libc/test/src/__support/wchar/utf8_to_32_test.cpp index 36ae7d689cc00..b419fb5d55414 100644 --- a/libc/test/src/__support/wchar/utf8_to_32_test.cpp +++ b/libc/test/src/__support/wchar/utf8_to_32_test.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "hdr/errno_macros.h" #include "src/__support/error_or.h" #include "src/__support/wchar/character_converter.h" #include "src/__support/wchar/mbstate.h" @@ -87,7 +88,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidByte) { LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); int err = char_conv.push(static_cast(ch)); - ASSERT_EQ(err, -1); + ASSERT_EQ(err, EILSEQ); } TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) { @@ -100,12 +101,12 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidMultiByte) { LIBC_NAMESPACE::internal::CharacterConverter char_conv(&state); int err = char_conv.push(static_cast(ch[0])); - ASSERT_EQ(err, -1); + ASSERT_EQ(err, EILSEQ); err = char_conv.push(static_cast(ch[1])); ASSERT_EQ(err, 0); // Prev byte was single byte so trying to push another should error. err = char_conv.push(static_cast(ch[2])); - ASSERT_EQ(err, -1); + ASSERT_EQ(err, EILSEQ); err = char_conv.push(static_cast(ch[3])); ASSERT_EQ(err, 0); } @@ -127,7 +128,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, InvalidLastByte) { err = char_conv.push(static_cast(ch[2])); ASSERT_EQ(err, 0); err = char_conv.push(static_cast(ch[3])); - ASSERT_EQ(err, -1); + ASSERT_EQ(err, EILSEQ); } TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) { @@ -144,7 +145,7 @@ TEST(LlvmLibcCharacterConverterUTF8To32Test, ValidTwoByteWithExtraRead) { ASSERT_EQ(err, 0); // Should produce an error on 3rd byte err = char_conv.push(static_cast(ch[2])); - ASSERT_EQ(err, -1); + ASSERT_EQ(err, EILSEQ); // Should produce an error since mbstate was reset auto wch = char_conv.pop_utf32();