From f1b04c6d865462beba66b640064ceb9c50e0260e Mon Sep 17 00:00:00 2001 From: Pavel Charvat Date: Sat, 9 Mar 2024 22:49:44 +0000 Subject: [PATCH] JSON: Don't generate corrupted output for string with surrogates. --- ucw-json/format.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/ucw-json/format.c b/ucw-json/format.c index 227a26b5..790fa15a 100644 --- a/ucw-json/format.c +++ b/ucw-json/format.c @@ -50,14 +50,16 @@ static void write_string(struct json_context *js, const char *p) bprintf(fb, "\\u%04x", u); } } - else if (u >= 0x007f && (js->format_options & JSON_FORMAT_ESCAPE_NONASCII)) + else if (u < 0x7f) + bputc(fb, u); + else if (u >= 0x110000 || u >= 0xd800 && u <= 0xdfff) + bprintf(fb, "\\u%04x", UNI_REPLACEMENT); + else if (js->format_options & JSON_FORMAT_ESCAPE_NONASCII) { if (u < 0x10000) bprintf(fb, "\\u%04x", u); - else if (u < 0x110000) - bprintf(fb, "\\u%04x\\u%04x", 0xd800 + ((u - 0x10000) >> 10), 0xdc00 + (u & 0x3ff)); else - bprintf(fb, "\\u%04x", UNI_REPLACEMENT); + bprintf(fb, "\\u%04x\\u%04x", 0xd800 + ((u - 0x10000) >> 10), 0xdc00 + (u & 0x3ff)); } else bput_utf8_32(fb, u); -- 2.39.2