From cb67b41a39afe3d001d7a36bf79e8333fcee50ae Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 7 Apr 2023 10:55:22 +0200 Subject: [PATCH] libc: Set errno in mbstowcs() and wcstombs() --- libc/src/stdlib.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) 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)