Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,14 @@ NOINLINE(static) VALUE json_string_unescape(JSON_ParserState *state, JSON_Parser
MEMCPY(buffer, p, char, stringEnd - p);
buffer += stringEnd - p;
}
rb_str_set_len(result, buffer - bufferStart);
// The buffer was sized for the raw (escaped) input, but the unescaped
// content can be much shorter (e.g. \uXXXX escapes: 6 raw bytes per at
// most 4 output bytes, 12 for a surrogate pair). rb_str_set_len would
// leave the raw size as capacity for the string's whole lifetime;
// rb_str_resize shrinks oversized buffers (embedding short strings and
// reallocating large ones to the exact length), so parsed strings only
// retain the bytes they actually hold.
rb_str_resize(result, (rb_len_t)(buffer - bufferStart));

if (symbolize) {
result = rb_str_intern(result);
Expand Down