Skip to content

Commit 02346d8

Browse files
committed
ResumableParser#partial_value avoid mutating the stacks
Fix: #1005 `parser.state.frames` and `parser.state.value_stack` need to be updated to point to the copy.
1 parent 2b908f6 commit 02346d8

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,9 @@ static VALUE cResumableParser_partial_value(VALUE self)
24962496
JSON_ResumableParser *original_parser = ResumableParser_acquire(self, false);
24972497
JSON_ResumableParser parser = *original_parser;
24982498

2499+
parser.state.frames = &parser.frames;
2500+
parser.state.value_stack = &parser.value_stack;
2501+
24992502
if (parser.value_stack.head == 0) {
25002503
return Qnil;
25012504
}

test/json/resumable_parser_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ def test_partial_value
156156
assert_partial_value([1, { "a" => 1, "b" => { "c" => nil } }], '[1, { "a": 1, "b": { "c"')
157157
end
158158

159+
def test_partial_value_issue_1005
160+
data = <<~JSON
161+
[
162+
[]
163+
]
164+
JSON
165+
data.each_line do |line|
166+
@parser << line
167+
@parser.parse
168+
@parser.partial_value # This unexpected parse error doesn't happen if we comment this out
169+
end
170+
assert_equal [[]], @parser.value
171+
end
172+
159173
def test_partial_value_missing
160174
assert_nil @parser.partial_value
161175
end

0 commit comments

Comments
 (0)