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
35 changes: 24 additions & 11 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2481,18 +2481,9 @@ static VALUE cResumableParser_clear(VALUE self)
return self;
}

/*
* call-seq: partial_value -> object
*
* Returns the Ruby objects parsed up to this point:
* parser << '[1, [2, 3,'
* parser.parse # => false
* parser.value # ArgumentError no ready value
* parser.partial_value # => [1, [2, 3]]
*/
static VALUE cResumableParser_partial_value(VALUE self)
static VALUE cResumableParser_partial_value_body(VALUE self)
{
JSON_ResumableParser *original_parser = ResumableParser_acquire(self, false);
JSON_ResumableParser *original_parser = cResumableParser_get(self);
JSON_ResumableParser parser = *original_parser;

parser.state.frames = &parser.frames;
Expand Down Expand Up @@ -2559,6 +2550,28 @@ static VALUE cResumableParser_partial_value(VALUE self)
return partial_result;
}

/*
* call-seq: partial_value -> object
*
* Returns the Ruby objects parsed up to this point:
* parser << '[1, [2, 3,'
* parser.parse # => false
* parser.value # ArgumentError no ready value
* parser.partial_value # => [1, [2, 3]]
*/
static VALUE cResumableParser_partial_value(VALUE self)
{
JSON_ResumableParser *parser = ResumableParser_acquire(self, true);

int status;
VALUE result = rb_protect(cResumableParser_partial_value_body, self, &status);
parser->in_use = false;
if (status) {
rb_jump_tag(status);
}
return result;
}

/*
* call-seq: rest -> string
*
Expand Down
20 changes: 20 additions & 0 deletions test/json/resumable_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,26 @@ def test_reentrency_prevented
assert_equal "ResumableParser can't be used recursively", error.message
end

def test_reentrency_prevented_in_partial_value
parser = nil
callback = ->(o) do
# Arrays are only built while partial_value runs (the scalars were pushed by the
# earlier parse); re-entering here used to corrupt/free the shared frame stack.
parser.parse if o.is_a?(Array)
o
end
parser = new_parser(on_load: callback)
parser << '[1, [2, 3,'
parser.parse
error = assert_raise ArgumentError do
parser.partial_value
end
assert_equal "ResumableParser can't be used recursively", error.message

# The in_use lock must be released even though partial_value raised.
refute_predicate parser, :value?
end

def test_exception_unlock_parser
called = false
parser = nil
Expand Down
Loading