diff --git a/libc/src/stdlib.cpp b/libc/src/stdlib.cpp index 6a880851..9a6ea3cd 100644 --- a/libc/src/stdlib.cpp +++ b/libc/src/stdlib.cpp @@ -107,23 +107,35 @@ extern "C" size_t mbstowcs(wchar_t* buf, const char* src, size_t max) { if (max == 0) return 0; + size_t result = (size_t)-1; Utf8StringDecoder decoder(src); - if (!buf) { return decoder.code_points().value_or((size_t)-1); } + if (!buf) + { + decoder.code_points().try_set_value_or_error(result, errno); + return result; + } - return decoder.decode(buf, max).value_or((size_t)-1); + decoder.decode(buf, max).try_set_value_or_error(result, errno); + return result; } size_t wcstombs(char* buf, const wchar_t* src, size_t max) { if (max == 0) return 0; + size_t result = (size_t)-1; Utf8StringEncoder encoder(src); - if (!buf) { return encoder.byte_length().value_or((size_t)-1); } + if (!buf) + { + encoder.byte_length().try_set_value_or_error(result, errno); + return result; + } - return encoder.encode(buf, max).value_or((size_t)-1); + encoder.encode(buf, max).try_set_value_or_error(result, errno); + return result; } void* malloc(size_t size)