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
18 changes: 2 additions & 16 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1693,26 +1693,12 @@ static VALUE cState_max_nesting(VALUE self)

static size_t max_nesting_config(VALUE num)
{
if (!RTEST(num)) return 0;
ssize_t max_nesting = NUM2SSIZET(num);
if (RB_UNLIKELY(max_nesting < 0)) {
rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting);
}
return (size_t)max_nesting;
return RTEST(num) ? json_num2size(num, "max_nesting") : 0;
}

// depth must never be negative; reject early with a clear error.
static size_t depth_config(VALUE num)
{
if (!RTEST(num)) return 0;
ssize_t d = NUM2SSIZET(num);
if (RB_UNLIKELY(d < 0)) {
rb_raise(rb_eArgError, "depth must be >= 0 (got %"PRIdSIZE")", d);
}
if (RB_UNLIKELY(d > INT_MAX)) {
rb_raise(rb_eArgError, "depth is too large (got %"PRIdSIZE")", d);
}
return (size_t)d;
return RTEST(num) ? json_num2size(num, "depth") : 0;
}

/*
Expand Down
9 changes: 9 additions & 0 deletions ext/json/ext/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,13 @@ static inline void raise_argument_error_on_unknown_keywords(VALUE unknown_keywor
}
}

static inline size_t json_num2size(VALUE num, const char *name)
{
int value = NUM2INT(num);
if (RB_UNLIKELY(value < 0)) {
rb_raise(rb_eArgError, "%s must be >= 0 (got %d)", name, value);
}
return (size_t)value;
}

#endif // _JSON_H_
7 changes: 1 addition & 6 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2011,12 +2011,7 @@ static void parser_config_wb_write(VALUE self, VALUE *dest, VALUE val)

static size_t max_nesting_config(VALUE num)
{
if (!RTEST(num)) return 0;
ssize_t max_nesting = NUM2SSIZET(num);
if (RB_UNLIKELY(max_nesting < 0)) {
rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting);
}
return (size_t)max_nesting;
return RTEST(num) ? json_num2size(num, "max_nesting") : 0;
}

static int parser_config_init_i(VALUE key, VALUE val, VALUE data)
Expand Down
11 changes: 4 additions & 7 deletions java/src/json/ext/GeneratorState.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public RubyInteger max_nesting_get(ThreadContext context) {
@JRubyMethod(name="max_nesting=")
public IRubyObject max_nesting_set(ThreadContext context, IRubyObject max_nesting) {
checkFrozen();
long value = RubyNumeric.num2long(max_nesting);
long value = RubyNumeric.num2int(max_nesting);
if (value < 0) {
throw context.runtime.newArgumentError("max_nesting must be >= 0 (got: " + value + ")");
}
Expand Down Expand Up @@ -562,7 +562,7 @@ public RubyInteger depth_get(ThreadContext context) {
@JRubyMethod(name="depth=")
public IRubyObject depth_set(ThreadContext context, IRubyObject vDepth) {
checkFrozen();
long value = checkDepth(context, RubyNumeric.num2long(vDepth));
long value = checkDepth(context, RubyNumeric.num2int(vDepth));
checkFrozen();
depth = value;
return vDepth;
Expand Down Expand Up @@ -613,7 +613,7 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) {
this.asJSON = proc;
}

maxNesting = opts.getLong("max_nesting", maxNesting);
maxNesting = opts.getInt("max_nesting", (int) maxNesting);
if (maxNesting < 0) {
throw context.runtime.newArgumentError("max_nesting must be >= 0 (got: " + maxNesting + ")");
}
Expand All @@ -623,7 +623,7 @@ public IRubyObject _configure(ThreadContext context, IRubyObject vOpts) {
strict = opts.getBool("strict", strict);
bufferInitialLength = opts.getLong("buffer_initial_length", bufferInitialLength);

depth = checkDepth(context, opts.getLong("depth", depth));
depth = checkDepth(context, opts.getInt("depth", (int) depth));
this.allowDuplicateKey = opts.getBool("allow_duplicate_key", allowDuplicateKey);

if (opts.hasKey("sort_keys")) sortKeys = normalizeSortKeys(context, opts.get("sort_keys"));
Expand Down Expand Up @@ -695,9 +695,6 @@ private static long checkDepth(ThreadContext context, long depth) {
if (depth < 0) {
throw context.runtime.newArgumentError("depth must be >= 0 (got: " + depth + ")");
}
if (depth > Integer.MAX_VALUE) {
throw context.runtime.newArgumentError("depth is too large (got: " + depth + ")");
}
return depth;
}

Expand Down
2 changes: 1 addition & 1 deletion java/src/json/ext/OptionsReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int getInt(String key, int defaultValue) {
IRubyObject value = get(key);
if (value == null) return defaultValue;
if (!value.isTrue()) return 0;
return RubyNumeric.fix2int(value);
return RubyNumeric.num2int(value);
}

long getLong(String key, long defaultValue) {
Expand Down
2 changes: 1 addition & 1 deletion java/src/json/ext/Parser.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions lib/json/truffle_ruby/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ def as_json=(value)
attr_reader :max_nesting

def max_nesting=(max_nesting)
max_nesting = max_nesting ? integer_config(max_nesting) : 0
if max_nesting.negative?
raise ArgumentError, "max_nesting must be >= 0 (got #{max_nesting})"
end
@max_nesting = max_nesting
@max_nesting = max_nesting ? nesting_config("max_nesting", max_nesting) : 0
end

# If this attribute is set to true, script tags and HTML comments will be escaped in
Expand Down Expand Up @@ -256,17 +252,26 @@ 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
@depth = depth ? nesting_config("depth", depth) : 0
end

private def integer_config(value)
Integer.try_convert(value) or raise TypeError, "no implicit conversion of #{value.class} into Integer"
end

# Mirrors NUM2INT in the C extension.
private def nesting_config(name, value)
value = integer_config(value)
if value > 2**31 - 1
raise RangeError, "integer #{value} too big to convert to 'int'"
elsif value < -(2**31)
raise RangeError, "integer #{value} too small to convert to 'int'"
elsif value.negative?
raise ArgumentError, "#{name} must be >= 0 (got #{value})"
end
value
end

def check_max_nesting # :nodoc:
return if @max_nesting.zero?
current_nesting = depth + 1
Expand Down Expand Up @@ -347,17 +352,13 @@ def configure(options)
allow_duplicate_key: @allow_duplicate_key, script_safe: @script_safe, strict: @strict,
max_nesting: @max_nesting, rfc8785: @rfc8785
)
if depth.negative?
raise ArgumentError, "depth must be >= 0 (got #{depth})"
end
depth = depth ? nesting_config("depth", depth) : 0

if max_nesting && !(Integer === max_nesting)
raise TypeError, ":max_nesting must be `false` or an Integer, got: #{max_nesting.class}"
end

if max_nesting && max_nesting.negative?
raise ArgumentError, "max_nesting must be >= 0 (got #{max_nesting})"
end
max_nesting = max_nesting ? nesting_config("max_nesting", max_nesting) : 0

@indent = indent || ''
@space = space || ''
Expand All @@ -377,7 +378,7 @@ def configure(options)
@allow_duplicate_key = allow_duplicate_key
@script_safe = script_safe
@strict = strict
@max_nesting = max_nesting || 0
@max_nesting = max_nesting
self.rfc8785 = rfc8785

validate_rfc8785
Expand Down
6 changes: 4 additions & 2 deletions test/json/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,19 +498,21 @@ def test_depth
end

def test_large_max_nesting
[2**62, 2**63 - 1].each do |value|
[2**30, 2**31 - 1].each do |value|
state = JSON::State.new
state.max_nesting = value
assert_equal value, state.max_nesting
assert_equal value, state.to_h[:max_nesting]
assert_equal value, JSON::State.new(max_nesting: value).max_nesting
end
assert_raise(RangeError) { JSON::State.new.max_nesting = 2**31 }
assert_raise(RangeError) { JSON::State.new(max_nesting: 2**31) }
end

def test_negative_max_nesting
assert_raise(ArgumentError) { JSON::State.new.max_nesting = -1 }
assert_raise(ArgumentError) { JSON::State.new(max_nesting: -1) }
assert_raise(ArgumentError) { JSON::State.new.configure(max_nesting: -(2**63)) }
assert_raise(RangeError) { JSON::State.new.configure(max_nesting: -(2**63)) }
assert_raise(ArgumentError) { JSON.generate([], max_nesting: -1) }
end

Expand Down
3 changes: 2 additions & 1 deletion test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,9 @@ def test_nesting
assert_equal too_deep_ary, ok
ok = parse too_deep, max_nesting: 0
assert_equal too_deep_ary, ok
ok = parse too_deep, max_nesting: 2**62
ok = parse too_deep, max_nesting: 2**31 - 1
assert_equal too_deep_ary, ok
assert_raise(RangeError) { parse too_deep, max_nesting: 2**31 }
assert_raise(ArgumentError) { parse too_deep, max_nesting: -1 }
end

Expand Down
Loading