diff --git a/libc/bionic/c16rtomb.cpp b/libc/bionic/c16rtomb.cpp index 2d6ae938c..e052d0470 100644 --- a/libc/bionic/c16rtomb.cpp +++ b/libc/bionic/c16rtomb.cpp @@ -43,7 +43,7 @@ static inline constexpr bool is_low_surrogate(char16_t c16) { size_t c16rtomb(char* s, char16_t c16, mbstate_t* ps) { static mbstate_t __private_state; mbstate_t* state = (ps == nullptr) ? &__private_state : ps; - if (mbsinit(state)) { + if (mbstate_is_initial(state)) { if (is_high_surrogate(c16)) { char32_t c32 = (c16 & ~0xd800) << 10; mbstate_set_byte(state, 3, (c32 & 0xff0000) >> 16); diff --git a/libc/bionic/c32rtomb.cpp b/libc/bionic/c32rtomb.cpp index 2909d8b36..d2519b96f 100644 --- a/libc/bionic/c32rtomb.cpp +++ b/libc/bionic/c32rtomb.cpp @@ -50,7 +50,7 @@ size_t c32rtomb(char* s, char32_t c32, mbstate_t* ps) { return mbstate_reset_and_return(1, state); } - if (!mbsinit(state)) { + if (!mbstate_is_initial(state)) { return mbstate_reset_and_return_illegal(EILSEQ, state); } diff --git a/libc/bionic/mbrtoc32.cpp b/libc/bionic/mbrtoc32.cpp index 644e54275..21603a120 100644 --- a/libc/bionic/mbrtoc32.cpp +++ b/libc/bionic/mbrtoc32.cpp @@ -55,7 +55,7 @@ size_t mbrtoc32(char32_t* pc32, const char* s, size_t n, mbstate_t* ps) { } uint8_t ch; - if (mbsinit(state) && (((ch = static_cast(*s)) & ~0x7f) == 0)) { + if (mbstate_is_initial(state) && (((ch = static_cast(*s)) & ~0x7f) == 0)) { // Fast path for plain ASCII characters. if (pc32 != nullptr) { *pc32 = ch; @@ -105,7 +105,7 @@ size_t mbrtoc32(char32_t* pc32, const char* s, size_t n, mbstate_t* ps) { size_t bytes_wanted = length - bytes_so_far; size_t i; for (i = 0; i < MIN(bytes_wanted, n); i++) { - if (!mbsinit(state) && ((*s & 0xc0) != 0x80)) { + if (!mbstate_is_initial(state) && ((*s & 0xc0) != 0x80)) { // Malformed input; bad characters in the middle of a character. return mbstate_reset_and_return_illegal(EILSEQ, state); } diff --git a/libc/bionic/wchar.cpp b/libc/bionic/wchar.cpp index dabe82485..bd9a45ee5 100644 --- a/libc/bionic/wchar.cpp +++ b/libc/bionic/wchar.cpp @@ -54,7 +54,7 @@ // int mbsinit(const mbstate_t* ps) { - return (ps == nullptr || (*(reinterpret_cast(ps->__seq)) == 0)); + return ps == nullptr || mbstate_is_initial(ps); } size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* ps) { @@ -148,7 +148,7 @@ size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstat static mbstate_t __private_state; mbstate_t* state = (ps == nullptr) ? &__private_state : ps; - if (!mbsinit(state)) { + if (!mbstate_is_initial(state)) { return mbstate_reset_and_return_illegal(EILSEQ, state); } diff --git a/libc/private/bionic_mbstate.h b/libc/private/bionic_mbstate.h index 352115aa7..243b2201b 100644 --- a/libc/private/bionic_mbstate.h +++ b/libc/private/bionic_mbstate.h @@ -44,6 +44,10 @@ __BEGIN_DECLS #define __MB_IS_ERR(rv) (rv == __MB_ERR_ILLEGAL_SEQUENCE || \ rv == __MB_ERR_INCOMPLETE_SEQUENCE) +static inline __wur bool mbstate_is_initial(const mbstate_t* ps) { + return *(reinterpret_cast(ps->__seq)) == 0; +} + static inline __wur size_t mbstate_bytes_so_far(const mbstate_t* ps) { return (ps->__seq[2] != 0) ? 3 :