From 4ef3ed4ba3c816203adb2f7c57f1b1d1bce16589 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 21 Sep 2021 21:28:53 +0200 Subject: [PATCH] LibC: Implement mbrtowc closer to POSIX --- Userland/Libraries/LibC/wchar.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Userland/Libraries/LibC/wchar.cpp b/Userland/Libraries/LibC/wchar.cpp index bf2f855b14..db8e5838c5 100644 --- a/Userland/Libraries/LibC/wchar.cpp +++ b/Userland/Libraries/LibC/wchar.cpp @@ -209,16 +209,11 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state) state = &_anonymous_state; } - // If s is nullptr, check if the state contains a complete multibyte character + // s being a null pointer is a shorthand for reading a single null byte. if (s == nullptr) { - if (mbstate_expected_bytes(state) == mbstate->stored_bytes) { - *state = {}; - return 0; - } else { - *state = {}; - errno = EILSEQ; - return -1; - } + pwc = nullptr; + s = ""; + n = 1; } // Stop early if we can't read anything @@ -284,6 +279,7 @@ size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* state) state->stored_bytes = 0; if (codepoint == 0) { + *state = {}; return 0; }