From d0600f57146b36afdd279964c7e72f31cb9ee9a5 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 14 Jan 2023 12:07:08 +0100 Subject: [PATCH] luna: Make Utf8StringEncoder short-circuit instead of failing when hitting the length limit --- luna/src/Utf8.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/luna/src/Utf8.cpp b/luna/src/Utf8.cpp index 269efdbd..9c9ea7ad 100644 --- a/luna/src/Utf8.cpp +++ b/luna/src/Utf8.cpp @@ -2,8 +2,6 @@ #include #include -// FIXME: Not enough space for a sequence is not an error. (mbstowcs(3) and wcstombs(3), case 2 when buf is not NULL) - static_assert(WCHAR_MAX > 0x10ffff); static Result utf8_char_length(char c) @@ -32,31 +30,31 @@ static inline usize wide_char_length_as_utf8_unchecked(wchar_t c) return 4; } -static Result encode_wide_char_as_utf8(wchar_t c, char* result, usize& len) +static Result encode_wide_char_as_utf8(wchar_t c, char* result, usize& len) { const usize utf8_len = TRY(wide_char_length_as_utf8(c)); - if (utf8_len > len) { return err(EILSEQ); } + if (utf8_len > len) { return false; } // Not enough space u8* buf = (u8*)result; if (len == 1) { buf[0] = c & 0x7f; - return {}; + return true; } if (len == 2) { buf[0] = 0b11000000 | ((c & 0x7c0) >> 6); buf[1] = 0b10000000 | (c & 0x3f); - return {}; + return true; } if (len == 3) { buf[0] = 0b11100000 | ((c & 0xf000) >> 12); buf[1] = 0b10000000 | ((c & 0xfc0) >> 6); buf[2] = 0b10000000 | (c & 0x3f); - return {}; + return true; } if (len == 4) { @@ -64,7 +62,7 @@ static Result encode_wide_char_as_utf8(wchar_t c, char* result, usize& len buf[1] = 0b10000000 | ((c & 0x3f000) >> 12); buf[2] = 0b10000000 | ((c & 0xfc0) >> 6); buf[3] = 0b10000000 | (c & 0x3f); - return {}; + return true; } unreachable(); @@ -197,7 +195,8 @@ Result Utf8StringEncoder::encode(char* buf, usize max) const while (*it && max > 1) { usize len = max - 1; - TRY(encode_wide_char_as_utf8(*it, buf, len)); + bool ok = TRY(encode_wide_char_as_utf8(*it, buf, len)); + if (!ok) break; buf += len; max -= len; it++;