From 3526defc1950274057f6b1b2d666751d68827268 Mon Sep 17 00:00:00 2001 From: Yijie Ma Date: Mon, 15 May 2023 15:38:03 -0700 Subject: [PATCH] [JsonWriter] Do not break in EscapeString when encountering a null byte (#33127) Instead just Utf-16 encode the null byte when dumping the value to a string form. --- src/core/lib/json/json_writer.cc | 4 +--- test/core/json/corpus/testcase-6037245441474560 | 1 + test/core/json/corpus/testcase-6329722588758016 | 1 + 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 test/core/json/corpus/testcase-6037245441474560 create mode 100644 test/core/json/corpus/testcase-6329722588758016 diff --git a/src/core/lib/json/json_writer.cc b/src/core/lib/json/json_writer.cc index 462a0d88b72..ef07c5e3925 100644 --- a/src/core/lib/json/json_writer.cc +++ b/src/core/lib/json/json_writer.cc @@ -145,9 +145,7 @@ void JsonWriter::EscapeString(const std::string& string) { OutputChar('"'); for (size_t idx = 0; idx < string.size(); ++idx) { uint8_t c = static_cast(string[idx]); - if (c == 0) { - break; - } else if (c >= 32 && c <= 126) { + if (c >= 32 && c <= 126) { if (c == '\\' || c == '"') OutputChar('\\'); OutputChar(static_cast(c)); } else if (c < 32 || c == 127) { diff --git a/test/core/json/corpus/testcase-6037245441474560 b/test/core/json/corpus/testcase-6037245441474560 new file mode 100644 index 00000000000..86e84f45682 --- /dev/null +++ b/test/core/json/corpus/testcase-6037245441474560 @@ -0,0 +1 @@ +"\u0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" \ No newline at end of file diff --git a/test/core/json/corpus/testcase-6329722588758016 b/test/core/json/corpus/testcase-6329722588758016 new file mode 100644 index 00000000000..ed6780dcbd6 --- /dev/null +++ b/test/core/json/corpus/testcase-6329722588758016 @@ -0,0 +1 @@ +"\u0000" \ No newline at end of file