From 0b3505e00fec69e95b7a84ee50f82d5fa485ad7c Mon Sep 17 00:00:00 2001 From: faumaray Date: Fri, 26 Jun 2026 02:39:10 +0300 Subject: [PATCH] Json: decode \u UTF-16 surrogate pairs to valid UTF-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The \uXXXX unescaper encoded each 16-bit unit independently, so an astral character written as a surrogate pair (e.g. an emoji, 🎸) became two separately-encoded surrogates — invalid UTF-8 (ED A0 BC ED BE B8) that renders as replacement characters. Consumers that validate UTF-8 (e.g. TeamSpeak) reject any string containing it, which silently dropped every bot reply that embedded an emoji-bearing YouTube title. Combine a high surrogate (D800-DBFF) with the following low surrogate (DC00-DFFF) into one code point and emit proper 4-byte UTF-8; a lone surrogate still falls through as before. Co-Authored-By: Claude Opus 4.8 --- include/Void/Data/Json.hpp | 60 +++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/include/Void/Data/Json.hpp b/include/Void/Data/Json.hpp index 3994c4a..5ef7fc6 100644 --- a/include/Void/Data/Json.hpp +++ b/include/Void/Data/Json.hpp @@ -580,23 +580,52 @@ namespace Void::Json break; case 'u': { - if (pos + 4 > src.size()) { - return std::unexpected{ - err("incomplete \\uXXXX")}; - } - const auto hex = src.substr(pos, 4); + auto read_hex4 = + [&](std::size_t at, + unsigned& out_cp) -> bool { + if (at + 4 > src.size()) { + return false; + } + const auto h = src.substr(at, 4); + const auto [p, e] = std::from_chars( + h.data(), h.data() + 4, out_cp, 16); + return e == std::errc{} && + p == h.data() + 4; + }; + unsigned cp = 0; - const auto [p, e] = std::from_chars( - hex.data(), hex.data() + 4, cp, 16); - if (e != std::errc{} || - p != hex.data() + 4) { + if (!read_hex4(pos, cp)) { return std::unexpected{ err("invalid \\uXXXX escape")}; } for (std::size_t i = 0; i < 4; ++i) { advance(); } - // Encode as UTF-8 + + // Combine a UTF-16 surrogate pair + // (\uD800-\uDBFF followed by \uDC00-\uDFFF) + // into one code point, so astral characters + // (emoji, …) become valid 4-byte UTF-8 + // instead of two invalid encoded surrogates. + if (cp >= 0xD800 && cp <= 0xDBFF) { + unsigned lo = 0; + if (pos + 1 < src.size() && + src[pos] == '\\' && + src[pos + 1] == 'u' && + read_hex4(pos + 2, lo) && + lo >= 0xDC00 && lo <= 0xDFFF) { + cp = 0x10000u + + ((cp - 0xD800u) << 10) + + (lo - 0xDC00u); + for (std::size_t i = 0; i < 6; ++i) { + advance(); + } + } + // A lone surrogate falls through and is + // encoded as-is. + } + + // Encode as UTF-8. if (cp < 0x80) { out += static_cast(cp); } else if (cp < 0x800) { @@ -604,13 +633,22 @@ namespace Void::Json static_cast(0xC0 | (cp >> 6)); out += static_cast(0x80 | (cp & 0x3F)); - } else { + } else if (cp < 0x10000) { out += static_cast(0xE0 | (cp >> 12)); out += static_cast( 0x80 | ((cp >> 6) & 0x3F)); out += static_cast(0x80 | (cp & 0x3F)); + } else { + out += static_cast(0xF0 | + (cp >> 18)); + out += static_cast( + 0x80 | ((cp >> 12) & 0x3F)); + out += static_cast( + 0x80 | ((cp >> 6) & 0x3F)); + out += static_cast(0x80 | + (cp & 0x3F)); } break; }