]> mj.ucw.cz Git - libucw.git/commitdiff
JSON: Don't generate corrupted output for string with surrogates. dev-json
authorPavel Charvat <pchar@ucw.cz>
Sat, 9 Mar 2024 22:49:44 +0000 (22:49 +0000)
committerPavel Charvat <pchar@ucw.cz>
Sat, 9 Mar 2024 22:49:44 +0000 (22:49 +0000)
ucw-json/format.c

index 227a26b548e7573c399f48b71d6701f15f77915f..790fa15aef20f841c08dd32a9cc4e6c75f7a6c88 100644 (file)
@@ -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);