Skip to content

Commit affc500

Browse files
authored
honor length in valueToQuotedString fast path (#1701)
1 parent b5ab350 commit affc500

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/lib_json/json_writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ static String valueToQuotedStringN(const char* value, size_t length,
216216
return "";
217217

218218
if (!doesAnyCharRequireEscaping(value, length))
219-
return String("\"") + value + "\"";
219+
return String("\"") + String(value, length) + "\"";
220220
// We have to walk value and escape any special characters.
221221
// Appending to String is not efficient, but this should be rare.
222222
// (Note: forward slashes are *not* rare, but I am not escaping them.)

src/test_lib_json/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,16 @@ JSONTEST_FIXTURE_LOCAL(StreamWriterTest, writeZeroes) {
28502850
}
28512851
}
28522852

2853+
// valueToQuotedString(value, length) must quote exactly `length` bytes and not
2854+
// walk off the end of a buffer that is not NUL-terminated at that length.
2855+
JSONTEST_FIXTURE_LOCAL(StreamWriterTest, quotedStringHonorsLength) {
2856+
// Bytes past position 5 must not leak into the output. Without honoring
2857+
// length the buffer is treated as a C-string and " world" is appended.
2858+
JSONTEST_ASSERT_STRING_EQUAL("\"hello\"",
2859+
Json::valueToQuotedString("hello world", 5));
2860+
JSONTEST_ASSERT_STRING_EQUAL("\"\"", Json::valueToQuotedString("abc", 0));
2861+
}
2862+
28532863
JSONTEST_FIXTURE_LOCAL(StreamWriterTest, unicode) {
28542864
// Create a Json value containing UTF-8 string with some chars that need
28552865
// escape (tab,newline).

0 commit comments

Comments
 (0)