Json: decode \u UTF-16 surrogate pairs to valid UTF-8
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
2d3f9dc319
commit
0b3505e00f
1 file changed
+49
-11
+49
-11
@@ -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<char>(cp);
|
||||
} else if (cp < 0x800) {
|
||||
@@ -604,13 +633,22 @@ namespace Void::Json
|
||||
static_cast<char>(0xC0 | (cp >> 6));
|
||||
out += static_cast<char>(0x80 |
|
||||
(cp & 0x3F));
|
||||
} else {
|
||||
} else if (cp < 0x10000) {
|
||||
out += static_cast<char>(0xE0 |
|
||||
(cp >> 12));
|
||||
out += static_cast<char>(
|
||||
0x80 | ((cp >> 6) & 0x3F));
|
||||
out += static_cast<char>(0x80 |
|
||||
(cp & 0x3F));
|
||||
} else {
|
||||
out += static_cast<char>(0xF0 |
|
||||
(cp >> 18));
|
||||
out += static_cast<char>(
|
||||
0x80 | ((cp >> 12) & 0x3F));
|
||||
out += static_cast<char>(
|
||||
0x80 | ((cp >> 6) & 0x3F));
|
||||
out += static_cast<char>(0x80 |
|
||||
(cp & 0x3F));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user