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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ The following default gems are updated.
* io-console 0.8.1
* io-nonblock 0.3.2
* io-wait 0.3.2
* json 2.15.1
* json 2.15.2
* openssl 4.0.0.pre
* optparse 0.7.0.dev.2
* pp 0.6.3
Expand Down
39 changes: 34 additions & 5 deletions ext/json/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ static inline long increase_depth(struct generate_json_data *data)
JSON_Generator_State *state = data->state;
long depth = ++state->depth;
if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", --state->depth);
}
return depth;
}
Expand Down Expand Up @@ -1491,10 +1491,39 @@ static VALUE cState_generate(int argc, VALUE *argv, VALUE self)
rb_check_arity(argc, 1, 2);
VALUE obj = argv[0];
VALUE io = argc > 1 ? argv[1] : Qnil;
VALUE result = cState_partial_generate(self, obj, generate_json, io);
return cState_partial_generate(self, obj, generate_json, io);
}

static VALUE cState_generate_new(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, 2);
VALUE obj = argv[0];
VALUE io = argc > 1 ? argv[1] : Qnil;

GET_STATE(self);
(void)state;
return result;

JSON_Generator_State new_state;
MEMCPY(&new_state, state, JSON_Generator_State, 1);

// FIXME: depth shouldn't be part of JSON_Generator_State, as that prevents it from being used concurrently.
new_state.depth = 0;

char stack_buffer[FBUFFER_STACK_SIZE];
FBuffer buffer = {
.io = RTEST(io) ? io : Qfalse,
};
fbuffer_stack_init(&buffer, state->buffer_initial_length, stack_buffer, FBUFFER_STACK_SIZE);

struct generate_json_data data = {
.buffer = &buffer,
.vstate = Qfalse,
.state = &new_state,
.obj = obj,
.func = generate_json
};
rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);

return fbuffer_finalize(&buffer);
}

static VALUE cState_initialize(int argc, VALUE *argv, VALUE self)
Expand Down Expand Up @@ -2072,7 +2101,7 @@ void Init_generator(void)
rb_define_method(cState, "buffer_initial_length", cState_buffer_initial_length, 0);
rb_define_method(cState, "buffer_initial_length=", cState_buffer_initial_length_set, 1);
rb_define_method(cState, "generate", cState_generate, -1);
rb_define_alias(cState, "generate_new", "generate"); // :nodoc:
rb_define_method(cState, "generate_new", cState_generate_new, -1); // :nodoc:

rb_define_private_method(cState, "allow_duplicate_key?", cState_allow_duplicate_key_p, 0);

Expand Down
2 changes: 1 addition & 1 deletion ext/json/lib/json/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module JSON
VERSION = '2.15.1'
VERSION = '2.15.2'
end
3 changes: 1 addition & 2 deletions include/ruby/internal/core/rtypeddata.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ struct RTypedData {
/**
* This is a `const rb_data_type_t *const` value, with the low bits set:
*
* 1: Always set, to differentiate RTypedData from RData.
* 2: Set if object is embedded.
* 1: Set if object is embedded.
*
* This field stores various information about how Ruby should handle a
* data. This roughly resembles a Ruby level class (apart from method
Expand Down
22 changes: 14 additions & 8 deletions lib/unicode_normalize/normalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,22 @@ def self.hangul_comp_one(string)

## Canonical Ordering
def self.canonical_ordering_one(string)
sorting = string.each_char.collect { |c| [c, CLASS_TABLE[c]] }
(sorting.length-2).downto(0) do |i| # almost, but not exactly bubble sort
(0..i).each do |j|
later_class = sorting[j+1].last
if 0<later_class and later_class<sorting[j].last
sorting[j], sorting[j+1] = sorting[j+1], sorting[j]
end
result = ''
unordered = []
chars = string.chars
n = chars.size
chars.each_with_index do |char, i|
ccc = CLASS_TABLE[char]
if ccc == 0
unordered.sort!.each { result << chars[it % n] }
unordered.clear
result << char
else
unordered << ccc * n + i
end
end
return sorting.collect(&:first).join('')
unordered.sort!.each { result << chars[it % n] }
result
end

## Normalization Forms for Patterns (not whole Strings)
Expand Down
10 changes: 10 additions & 0 deletions test/json/json_coder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,14 @@ def test_json_coder_dump_NaN_or_Infinity_loop
end
assert_include error.message, "NaN not allowed in JSON"
end

def test_nesting_recovery
coder = JSON::Coder.new
ary = []
ary << ary
assert_raise JSON::NestingError do
coder.dump(ary)
end
assert_equal '{"a":1}', coder.dump({ a: 1 })
end
end
19 changes: 19 additions & 0 deletions test/test_unicode_normalize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,23 @@ def test_us_ascii
assert_equal true, ascii_string.unicode_normalized?(:nfkc)
assert_equal true, ascii_string.unicode_normalized?(:nfkd)
end

def test_canonical_ordering
a = "\u03B1\u0313\u0300\u0345"
a_unordered1 = "\u03B1\u0345\u0313\u0300"
a_unordered2 = "\u03B1\u0313\u0345\u0300"
u1 = "U\u0308\u0304"
u2 = "U\u0304\u0308"
s = "s\u0323\u0307"
s_unordered = "s\u0307\u0323"
o = "\u{1611e}\u{1611e}\u{1611f}"
# Actual cases called through String#unicode_normalize
assert_equal(s + o, UnicodeNormalize.canonical_ordering_one(s_unordered + o))
assert_equal(a[1..], UnicodeNormalize.canonical_ordering_one(a_unordered1[1..]))
assert_equal(a[1..] + o, UnicodeNormalize.canonical_ordering_one(a_unordered2[1..] + o))
# Artificial cases
assert_equal(a + u1 + o + u2 + s, UnicodeNormalize.canonical_ordering_one(a + u1 + o + u2 + s))
assert_equal(s[1..] + a + a, UnicodeNormalize.canonical_ordering_one(s_unordered[1..] + a_unordered1 + a_unordered2))
assert_equal(o + s + u1 + a + o + a + u2 + o, UnicodeNormalize.canonical_ordering_one(o + s_unordered + u1 + a_unordered1 + o + a_unordered2 + u2 + o))
end
end