diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c index 17b268b4..81e7d501 100644 --- a/ext/json/ext/generator/generator.c +++ b/ext/json/ext/generator/generator.c @@ -1662,7 +1662,9 @@ static VALUE cState_as_json_set(VALUE self, VALUE as_json) { rb_check_frozen(self); GET_STATE(self); - RB_OBJ_WRITE(self, &state->as_json, as_json_config(as_json)); + VALUE proc = as_json_config(as_json); + rb_check_frozen(self); + RB_OBJ_WRITE(self, &state->as_json, proc); return Qnil; } @@ -1724,7 +1726,9 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth) { rb_check_frozen(self); GET_STATE(self); - state->max_nesting = max_nesting_config(depth); + size_t value = max_nesting_config(depth); + rb_check_frozen(self); + state->max_nesting = value; return Qnil; } @@ -1962,7 +1966,9 @@ static VALUE cState_depth_set(VALUE self, VALUE depth) { rb_check_frozen(self); GET_STATE(self); - state->depth = depth_config(depth); + size_t value = depth_config(depth); + rb_check_frozen(self); + state->depth = value; return Qnil; } @@ -2015,6 +2021,13 @@ static inline void state_write_value(struct configure_state_data *data, VALUE *f } } +// Option conversions can call user code (`to_proc`, `to_int`) which may +// freeze the receiver, so recheck right before writing. +static inline void configure_check_frozen(struct configure_state_data *data) +{ + if (RTEST(data->vstate)) rb_check_frozen(data->vstate); +} + static int configure_state_i(VALUE key, VALUE val, VALUE _arg) { struct configure_state_data *data = (struct configure_state_data *)_arg; @@ -2025,10 +2038,18 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg) else if (key == sym_space_before) { state_write_value(data, &state->space_before, string_config(val)); } else if (key == sym_object_nl) { state_write_value(data, &state->object_nl, string_config(val)); } else if (key == sym_array_nl) { state_write_value(data, &state->array_nl, string_config(val)); } - else if (key == sym_max_nesting) { state->max_nesting = max_nesting_config(val); } + else if (key == sym_max_nesting) { + size_t max_nesting = max_nesting_config(val); + configure_check_frozen(data); + state->max_nesting = max_nesting; + } else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); } else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); } - else if (key == sym_depth) { state->depth = depth_config(val); } + else if (key == sym_depth) { + size_t depth = depth_config(val); + configure_check_frozen(data); + state->depth = depth; + } else if (key == sym_buffer_initial_length) { buffer_initial_length_set(state, val); } else if (key == sym_script_safe) { state->script_safe = RTEST(val); } else if (key == sym_strict) { state->strict = RTEST(val); } @@ -2036,6 +2057,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg) else if (key == sym_rfc8785) { state->rfc8785 = RTEST(val); } else if (key == sym_as_json) { VALUE proc = RTEST(val) ? as_json_config(val) : Qfalse; + configure_check_frozen(data); state->as_json_single_arg = proc && rb_proc_arity(proc) == 1; state_write_value(data, &state->as_json, proc); } diff --git a/java/src/json/ext/GeneratorState.java b/java/src/json/ext/GeneratorState.java index dd7eafef..81a20d79 100644 --- a/java/src/json/ext/GeneratorState.java +++ b/java/src/json/ext/GeneratorState.java @@ -386,7 +386,9 @@ public IRubyObject as_json_set(ThreadContext context, IRubyObject asJSON) { if (asJSON.isNil() || asJSON == context.getRuntime().getFalse()) { this.asJSON = null; } else { - this.asJSON = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc"); + RubyProc proc = (RubyProc)TypeConverter.convertToType(asJSON, context.getRuntime().getProc(), "to_proc"); + checkFrozen(); + this.asJSON = proc; } return asJSON; } @@ -408,6 +410,7 @@ public IRubyObject max_nesting_set(ThreadContext context, IRubyObject max_nestin if (value < 0) { throw context.runtime.newArgumentError("max_nesting must be >= 0 (got: " + value + ")"); } + checkFrozen(); maxNesting = value; return max_nesting; } @@ -559,7 +562,9 @@ public RubyInteger depth_get(ThreadContext context) { @JRubyMethod(name="depth=") public IRubyObject depth_set(ThreadContext context, IRubyObject vDepth) { checkFrozen(); - depth = checkDepth(context, RubyNumeric.num2long(vDepth)); + long value = checkDepth(context, RubyNumeric.num2long(vDepth)); + checkFrozen(); + depth = value; return vDepth; } @@ -602,7 +607,11 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) { this.arrayNl = stringConfig(opts, "array_nl", this.arrayNl); this.objectNl = stringConfig(opts, "object_nl", this.objectNl); - if (opts.hasKey("as_json")) this.asJSON = opts.getProc("as_json"); + if (opts.hasKey("as_json")) { + RubyProc proc = opts.getProc("as_json"); + checkFrozen(); + this.asJSON = proc; + } maxNesting = opts.getLong("max_nesting", maxNesting); if (maxNesting < 0) { diff --git a/lib/json/truffle_ruby/generator.rb b/lib/json/truffle_ruby/generator.rb index 5111817d..2fe6e897 100644 --- a/lib/json/truffle_ruby/generator.rb +++ b/lib/json/truffle_ruby/generator.rb @@ -196,7 +196,7 @@ def as_json=(value) attr_reader :max_nesting def max_nesting=(max_nesting) - max_nesting ||= 0 + max_nesting = max_nesting ? integer_config(max_nesting) : 0 if max_nesting.negative? raise ArgumentError, "max_nesting must be >= 0 (got #{max_nesting})" end @@ -256,12 +256,17 @@ def buffer_initial_length=(length) attr_reader :depth def depth=(depth) + depth = depth ? integer_config(depth) : 0 if depth.negative? raise ArgumentError, "depth must be >= 0 (got #{depth})" end @depth = depth end + private def integer_config(value) + Integer.try_convert(value) or raise TypeError, "no implicit conversion of #{value.class} into Integer" + end + def check_max_nesting # :nodoc: return if @max_nesting.zero? current_nesting = depth + 1 diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb index 8e613301..810447e2 100755 --- a/test/json/json_generator_test.rb +++ b/test/json/json_generator_test.rb @@ -731,6 +731,38 @@ def test_initialize_copy_frozen_receiver assert_equal 'original', state.clone.indent end + def test_as_json_to_proc_freezing_receiver + state = JSON::State.new + proxy = Object.new + proxy.define_singleton_method(:to_proc) do + state.freeze + proc { |object, _key| object.to_s } + end + + assert_raise(FrozenError) { state.as_json = proxy } + assert_equal false, state.as_json + + state = JSON::State.new + assert_raise(FrozenError) { state.configure(as_json: proxy) } + assert_equal false, state.as_json + end + + def test_integer_option_to_int_freezing_receiver + state = JSON::State.new + value = Object.new + value.define_singleton_method(:to_int) do + state.freeze + 42 + end + + assert_raise(FrozenError) { state.max_nesting = value } + assert_equal 100, state.max_nesting + + state = JSON::State.new + assert_raise(FrozenError) { state.depth = value } + assert_equal 0, state.depth + end + def test_json_state_to_h_roundtrip state = JSON.state.new assert_equal state.to_h, JSON.state.new(state.to_h).to_h