Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2487,6 +2487,9 @@ static VALUE cResumableParser_clear(VALUE self)
parser->frames.head = 0;
parser->value_stack.head = 0;
parser->state.name_cache.length = 0;
parser->state.current_nesting = 0;
parser->state.in_array = 1;
parser->state.emitted_deprecations = 0;
Comment on lines +2490 to +2492

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should just memset(parser, 0, sizeof(*parser)).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess not. Because then we need to be careful not to leak the buffers we allocated etc. Nevermind.

parser->state.start = parser->state.cursor = parser->state.end = NULL;
return self;
}
Expand Down
14 changes: 14 additions & 0 deletions test/json/resumable_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ def test_parse_with_empty_buffer_keeps_parser_usable
assert_equal [4], @parser.value
end

def test_clear_resets_nesting_depth
# An unfinished document leaks a nesting level; #clear must reset it so a later shallow
# document is not rejected with a spurious NestingError.
parser = new_parser(max_nesting: 10)
10.times do
parser << '[1' # opens an array that is never closed before clear
parser.parse
parser.clear
end
parser << '[1]'
assert parser.parse
assert_equal [1], parser.value
end

def test_parse_document_direct
@parser << '[true]'
assert_equal true, @parser.parse
Expand Down
Loading