From f4bd4361d477afb0775cb5b5e04eaf4def42b66f Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 15 Jun 2026 16:05:03 +0200 Subject: [PATCH] ResumableParser#partial_value avoid mutating the stacks Fix: https://github.com/ruby/json/issues/1005 `parser.state.frames` and `parser.state.value_stack` need to be updated to point to the copy. --- ext/json/ext/parser/parser.c | 3 +++ test/json/resumable_parser_test.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 15e79b24..e722a1a7 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -2496,6 +2496,9 @@ static VALUE cResumableParser_partial_value(VALUE self) JSON_ResumableParser *original_parser = ResumableParser_acquire(self, false); JSON_ResumableParser parser = *original_parser; + parser.state.frames = &parser.frames; + parser.state.value_stack = &parser.value_stack; + if (parser.value_stack.head == 0) { return Qnil; } diff --git a/test/json/resumable_parser_test.rb b/test/json/resumable_parser_test.rb index bab92c8d..600cd33e 100644 --- a/test/json/resumable_parser_test.rb +++ b/test/json/resumable_parser_test.rb @@ -156,6 +156,20 @@ def test_partial_value assert_partial_value([1, { "a" => 1, "b" => { "c" => nil } }], '[1, { "a": 1, "b": { "c"') end + def test_partial_value_issue_1005 + data = <<~JSON + [ + [] + ] + JSON + data.each_line do |line| + @parser << line + @parser.parse + @parser.partial_value # This unexpected parse error doesn't happen if we comment this out + end + assert_equal [[]], @parser.value + end + def test_partial_value_missing assert_nil @parser.partial_value end