Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Range fix #811

Merged
merged 2 commits into from
Aug 23, 2022
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
28 changes: 16 additions & 12 deletions ext/oj/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,26 +333,30 @@ static int hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, vol
// If struct is not defined then we let this fail and raise an exception.
sc = oj_name2struct(pi, *RARRAY_PTR(value), rb_eArgError);
}
// Create a properly initialized struct instance without calling the initialize method.
parent->val = rb_obj_alloc(sc);
// If the JSON array has more entries than the struct class allows, we record an error.
if (sc == rb_cRange) {
parent->val = rb_class_new_instance(len - 1, RARRAY_PTR(value) + 1, rb_cRange);
} else {
// Create a properly initialized struct instance without calling the initialize method.
parent->val = rb_obj_alloc(sc);
// If the JSON array has more entries than the struct class allows, we record an error.
#ifdef RSTRUCT_LEN
#if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
slen = (int)NUM2LONG(RSTRUCT_LEN(parent->val));
#else // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
slen = (int)RSTRUCT_LEN(parent->val);
slen = (int)RSTRUCT_LEN(parent->val);
#endif // RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
#else
slen = FIX2INT(rb_funcall2(parent->val, oj_length_id, 0, 0));
slen = FIX2INT(rb_funcall2(parent->val, oj_length_id, 0, 0));
#endif
// MRI >= 1.9
if (len - 1 > slen) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
} else {
int i;
// MRI >= 1.9
if (len - 1 > slen) {
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "Invalid struct data");
} else {
int i;

for (i = 0; i < len - 1; i++) {
rb_struct_aset(parent->val, INT2FIX(i), RARRAY_PTR(value)[i + 1]);
for (i = 0; i < len - 1; i++) {
rb_struct_aset(parent->val, INT2FIX(i), RARRAY_PTR(value)[i + 1]);
}
}
}
return 1;
Expand Down
2 changes: 1 addition & 1 deletion ext/oj/oj.c
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ static int parse_options_cb(VALUE k, VALUE v, VALUE opts) {
if (Qnil == v) {
return ST_CONTINUE;
}
if (TYPE(v) == T_STRUCT && rb_obj_class(v) == rb_cRange) {
if (rb_obj_class(v) == rb_cRange) {
VALUE min = rb_funcall(v, oj_begin_id, 0);
VALUE max = rb_funcall(v, oj_end_id, 0);

Expand Down
6 changes: 0 additions & 6 deletions test/test_integer_range.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,18 @@ def teardown
end

def test_range
skip 'TruffleRuby fails this spec with `ArgumentError: :integer_range must be a range of Fixnum.`' if RUBY_ENGINE == 'truffleruby'

test = {s: 0, s2: -1, s3: 1, u: -2, u2: 2, u3: 9007199254740993}
exp = '{"s":0,"s2":-1,"s3":1,"u":"-2","u2":"2","u3":"9007199254740993"}'
assert_equal(exp, Oj.dump(test, integer_range: (-1..1)))
end

def test_bignum
skip 'TruffleRuby fails this spec with `ArgumentError: :integer_range must be a range of Fixnum.`' if RUBY_ENGINE == 'truffleruby'

test = {u: -10000000000000000000, u2: 10000000000000000000}
exp = '{"u":"-10000000000000000000","u2":"10000000000000000000"}'
assert_equal(exp, Oj.dump(test, integer_range: (-1..1)))
end

def test_valid_modes
skip 'TruffleRuby fails this spec with `ArgumentError: :integer_range must be a range of Fixnum.`' if RUBY_ENGINE == 'truffleruby'

test = {safe: 0, unsafe: 9007199254740993}
exp = '{"safe":0,"unsafe":"9007199254740993"}'

Expand Down