libc: Set errno in mbstowcs() and wcstombs()

This commit is contained in:
apio 2023-04-07 10:55:22 +02:00
parent 1f0e185904
commit cb67b41a39
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -107,23 +107,35 @@ extern "C"
size_t mbstowcs(wchar_t* buf, const char* src, size_t max) size_t mbstowcs(wchar_t* buf, const char* src, size_t max)
{ {
if (max == 0) return 0; if (max == 0) return 0;
size_t result = (size_t)-1;
Utf8StringDecoder decoder(src); 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) size_t wcstombs(char* buf, const wchar_t* src, size_t max)
{ {
if (max == 0) return 0; if (max == 0) return 0;
size_t result = (size_t)-1;
Utf8StringEncoder encoder(src); 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) void* malloc(size_t size)