Skip to content

Commit

Permalink
Try out different max_nesting check
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed Aug 15, 2023
1 parent 1995a13 commit 945a397
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
13 changes: 13 additions & 0 deletions ext/oj/dump_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,12 @@ void oj_dump_compat_val(VALUE obj, int depth, Out out, bool as_ok) {
int type = rb_type(obj);

TRACE(out->opts->trace, "dump", obj, depth, TraceIn);
// The max_nesting logic is that an empty Array or Hash is assumed to have
// content so the max_nesting should fail but a non-collection value is
// okay. That means a check for a collectable value is needed before
// raising.
if (out->opts->dump_opts.max_depth <= depth) {
#if 0
// When JSON.dump is called then an ArgumentError is expected and the
// limit is the depth inclusive. If JSON.generate is called then a
// NestingError is expected and the limit is inclusive. Worse than
Expand All @@ -886,6 +891,14 @@ void oj_dump_compat_val(VALUE obj, int depth, Out out, bool as_ok) {
}
raise_json_err("Too deeply nested", "NestingError");
}
#else
if (RUBY_T_ARRAY == type || RUBY_T_HASH == type) {
if (0 < out->argc) {
set_state_depth(*out->argv, depth);
}
raise_json_err("Too deeply nested", "NestingError");
}
#endif
}
if (0 < type && type <= RUBY_T_FIXNUM) {
DumpFunc f = compat_funcs[type];
Expand Down
6 changes: 6 additions & 0 deletions notes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
- skip_null_byte => omit_null_byte
- and move to dump parameters

- max_nesting
- JSON.dump([[]], n) - n = 2 ok, n = 1 fails
- generate is the same
- fast_generate is the same
- pretty_generate is the same
- [[]].to_json same


- stream writes
Expand Down
2 changes: 1 addition & 1 deletion test/json_gem/json_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def test_nesting
too_deep_ary = eval too_deep
assert_raise(JSON::NestingError) { JSON.generate too_deep_ary }
assert_raise(JSON::NestingError) { JSON.generate too_deep_ary, :max_nesting => 100 }
ok = JSON.generate too_deep_ary, :max_nesting => 101
ok = JSON.generate too_deep_ary, :max_nesting => 102
assert_equal too_deep, ok
ok = JSON.generate too_deep_ary, :max_nesting => nil
assert_equal too_deep, ok
Expand Down
13 changes: 13 additions & 0 deletions test/test_compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ def test_arg_passing
def test_max_nesting
assert_raises() { Oj.to_json([[[[[]]]]], :max_nesting => 3) }
assert_raises() { Oj.dump([[[[[]]]]], :max_nesting => 3, :mode=>:compat) }

assert_raises() { Oj.to_json([[]], :max_nesting => 1) }
assert_equal('[[]]', Oj.to_json([[]], :max_nesting => 2))

assert_raises() { Oj.dump([[]], :max_nesting => 1, :mode=>:compat) }
assert_equal('[[]]', Oj.dump([[]], :max_nesting => 2, :mode=>:compat))

assert_raises() { Oj.to_json([[3]], :max_nesting => 1) }
assert_equal('[[3]]', Oj.to_json([[3]], :max_nesting => 2))

assert_raises() { Oj.dump([[3]], :max_nesting => 1, :mode=>:compat) }
assert_equal('[[3]]', Oj.dump([[3]], :max_nesting => 2, :mode=>:compat))

end

def test_bad_unicode
Expand Down

0 comments on commit 945a397

Please sign in to comment.