Skip to content

Commit 500738e

Browse files
committed
Disallow usage of symbolize_names and create_additions
1 parent 68ce5b6 commit 500738e

5 files changed

Lines changed: 35 additions & 18 deletions

File tree

ext/json/ext/parser/parser.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,8 +1681,9 @@ static VALUE convert_encoding(VALUE source)
16811681
* defiance of RFC 4627 to be parsed by the Parser. This option defaults to
16821682
* false.
16831683
* * *symbolize_names*: If set to true, returns symbols for the names
1684-
* (keys) in a JSON object. Otherwise strings are returned, which is also
1685-
* the default.
1684+
* (keys) in a JSON object. Otherwise strings are returned, which is
1685+
* also the default. It's not possible to use this option in
1686+
* conjunction with the *create_additions* option.
16861687
* * *create_additions*: If set to false, the Parser doesn't create
16871688
* additions even if a matching class and create_id was found. This option
16881689
* defaults to false.
@@ -1733,6 +1734,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
17331734
} else {
17341735
json->create_additions = 0;
17351736
}
1737+
if (json->symbolize_names && json->create_additions) {
1738+
rb_raise(rb_eArgError,
1739+
"options :symbolize_names and :create_additions cannot be "
1740+
" used in conjunction");
1741+
}
17361742
tmp = ID2SYM(i_create_id);
17371743
if (option_given_p(opts, tmp)) {
17381744
json->create_id = rb_hash_aref(opts, tmp);
@@ -1778,15 +1784,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
17781784
}
17791785

17801786

1781-
#line 1782 "parser.c"
1787+
#line 1788 "parser.c"
17821788
static const int JSON_start = 1;
17831789
static const int JSON_first_final = 10;
17841790
static const int JSON_error = 0;
17851791

17861792
static const int JSON_en_main = 1;
17871793

17881794

1789-
#line 690 "parser.rl"
1795+
#line 696 "parser.rl"
17901796

17911797

17921798
/*
@@ -1803,16 +1809,16 @@ static VALUE cParser_parse(VALUE self)
18031809
GET_PARSER;
18041810

18051811

1806-
#line 1807 "parser.c"
1812+
#line 1813 "parser.c"
18071813
{
18081814
cs = JSON_start;
18091815
}
18101816

1811-
#line 706 "parser.rl"
1817+
#line 712 "parser.rl"
18121818
p = json->source;
18131819
pe = p + json->len;
18141820

1815-
#line 1816 "parser.c"
1821+
#line 1822 "parser.c"
18161822
{
18171823
if ( p == pe )
18181824
goto _test_eof;
@@ -1846,7 +1852,7 @@ case 1:
18461852
cs = 0;
18471853
goto _out;
18481854
tr2:
1849-
#line 682 "parser.rl"
1855+
#line 688 "parser.rl"
18501856
{
18511857
char *np = JSON_parse_value(json, p, pe, &result);
18521858
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1856,7 +1862,7 @@ cs = 0;
18561862
if ( ++p == pe )
18571863
goto _test_eof10;
18581864
case 10:
1859-
#line 1860 "parser.c"
1865+
#line 1866 "parser.c"
18601866
switch( (*p) ) {
18611867
case 13: goto st10;
18621868
case 32: goto st10;
@@ -1945,7 +1951,7 @@ case 9:
19451951
_out: {}
19461952
}
19471953

1948-
#line 709 "parser.rl"
1954+
#line 715 "parser.rl"
19491955

19501956
if (cs >= JSON_first_final && p == pe) {
19511957
return result;

ext/json/ext/parser/parser.rl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,9 @@ static VALUE convert_encoding(VALUE source)
576576
* defiance of RFC 4627 to be parsed by the Parser. This option defaults to
577577
* false.
578578
* * *symbolize_names*: If set to true, returns symbols for the names
579-
* (keys) in a JSON object. Otherwise strings are returned, which is also
580-
* the default.
579+
* (keys) in a JSON object. Otherwise strings are returned, which is
580+
* also the default. It's not possible to use this option in
581+
* conjunction with the *create_additions* option.
581582
* * *create_additions*: If set to false, the Parser doesn't create
582583
* additions even if a matching class and create_id was found. This option
583584
* defaults to false.
@@ -628,6 +629,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
628629
} else {
629630
json->create_additions = 0;
630631
}
632+
if (json->symbolize_names && json->create_additions) {
633+
rb_raise(rb_eArgError,
634+
"options :symbolize_names and :create_additions cannot be "
635+
" used in conjunction");
636+
}
631637
tmp = ID2SYM(i_create_id);
632638
if (option_given_p(opts, tmp)) {
633639
json->create_id = rb_hash_aref(opts, tmp);

lib/json/pure/parser.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ class Parser < StringScanner
6161
# defiance of RFC 4627 to be parsed by the Parser. This option defaults
6262
# to false.
6363
# * *symbolize_names*: If set to true, returns symbols for the names
64-
# (keys) in a JSON object. Otherwise strings are returned, which is also
65-
# the default.
64+
# (keys) in a JSON object. Otherwise strings are returned, which is
65+
# also the default. It's not possible to use this option in
66+
# conjunction with the *create_additions* option.
6667
# * *create_additions*: If set to true, the Parser creates
6768
# additions when if a matching class and create_id was found. This
6869
# option defaults to false.
@@ -90,6 +91,9 @@ def initialize(source, opts = {})
9091
else
9192
@create_additions = false
9293
end
94+
@symbolize_names && @create_additions and raise ArgumentError,
95+
'options :symbolize_names and :create_additions cannot be used '\
96+
'in conjunction'
9397
@create_id = @create_additions ? JSON.create_id : nil
9498
@object_class = opts[:object_class] || Hash
9599
@array_class = opts[:array_class] || Array

tests/json_common_interface_test.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,8 @@ def test_load
6767
end
6868

6969
def test_load_with_options
70-
small_hash = JSON("foo" => 'bar')
71-
symbol_hash = { :foo => 'bar' }
72-
assert_equal symbol_hash,
73-
JSON.load(small_hash, nil, :symbolize_names => true)
70+
json = '{ "foo": NaN }'
71+
assert JSON.load(json, nil, :allow_nan => true)['foo'].nan?
7472
end
7573

7674
def test_dump

tests/json_parser_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ def test_symbolize_names
183183
parse('{"foo":"bar", "baz":"quux"}'))
184184
assert_equal({ :foo => "bar", :baz => "quux" },
185185
parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
186+
assert_raise(ArgumentError) do
187+
parse('{}', :symbolize_names => true, :create_additions => true)
188+
end
186189
end
187190

188191
def test_parse_comments

0 commit comments

Comments
 (0)