diff options
-rw-r--r-- | ext/json/ext/parser/parser.c | 35 | ||||
-rw-r--r-- | ext/json/ext/parser/parser.rl | 1 | ||||
-rw-r--r-- | lib/json/common.rb | 2 | ||||
-rw-r--r-- | lib/json/pure/parser.rb | 9 | ||||
-rwxr-xr-x | tests/test_json.rb | 2 | ||||
-rwxr-xr-x | tests/test_json_generate.rb | 20 |
6 files changed, 49 insertions, 20 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 18ee58e..b44b396 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -1714,6 +1714,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->object_class = Qnil; json->array_class = Qnil; } + source = rb_convert_type(source, T_STRING, "String", "to_str"); if (!json->quirks_mode) { source = convert_encoding(StringValue(source)); } @@ -1725,7 +1726,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) } -#line 1729 "parser.c" +#line 1730 "parser.c" static const int JSON_start = 1; static const int JSON_first_final = 10; static const int JSON_error = 0; @@ -1733,7 +1734,7 @@ static const int JSON_error = 0; static const int JSON_en_main = 1; -#line 736 "parser.rl" +#line 737 "parser.rl" static VALUE cParser_parse_strict(VALUE self) @@ -1744,16 +1745,16 @@ static VALUE cParser_parse_strict(VALUE self) GET_PARSER; -#line 1748 "parser.c" +#line 1749 "parser.c" { cs = JSON_start; } -#line 746 "parser.rl" +#line 747 "parser.rl" p = json->source; pe = p + json->len; -#line 1757 "parser.c" +#line 1758 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1809,7 +1810,7 @@ case 5: goto st1; goto st5; tr3: -#line 725 "parser.rl" +#line 726 "parser.rl" { char *np; json->current_nesting = 1; @@ -1818,7 +1819,7 @@ tr3: } goto st10; tr4: -#line 718 "parser.rl" +#line 719 "parser.rl" { char *np; json->current_nesting = 1; @@ -1830,7 +1831,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1834 "parser.c" +#line 1835 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -1887,7 +1888,7 @@ case 9: _out: {} } -#line 749 "parser.rl" +#line 750 "parser.rl" if (cs >= JSON_first_final && p == pe) { return result; @@ -1899,7 +1900,7 @@ case 9: -#line 1903 "parser.c" +#line 1904 "parser.c" static const int JSON_quirks_mode_start = 1; static const int JSON_quirks_mode_first_final = 10; static const int JSON_quirks_mode_error = 0; @@ -1907,7 +1908,7 @@ static const int JSON_quirks_mode_error = 0; static const int JSON_quirks_mode_en_main = 1; -#line 774 "parser.rl" +#line 775 "parser.rl" static VALUE cParser_parse_quirks_mode(VALUE self) @@ -1918,16 +1919,16 @@ static VALUE cParser_parse_quirks_mode(VALUE self) GET_PARSER; -#line 1922 "parser.c" +#line 1923 "parser.c" { cs = JSON_quirks_mode_start; } -#line 784 "parser.rl" +#line 785 "parser.rl" p = json->source; pe = p + json->len; -#line 1931 "parser.c" +#line 1932 "parser.c" { if ( p == pe ) goto _test_eof; @@ -1961,7 +1962,7 @@ st0: cs = 0; goto _out; tr2: -#line 766 "parser.rl" +#line 767 "parser.rl" { char *np = JSON_parse_value(json, p, pe, &result); if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;} @@ -1971,7 +1972,7 @@ st10: if ( ++p == pe ) goto _test_eof10; case 10: -#line 1975 "parser.c" +#line 1976 "parser.c" switch( (*p) ) { case 13: goto st10; case 32: goto st10; @@ -2060,7 +2061,7 @@ case 9: _out: {} } -#line 787 "parser.rl" +#line 788 "parser.rl" if (cs >= JSON_quirks_mode_first_final && p == pe) { return result; diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl index 2a2cc89..56e1a11 100644 --- a/ext/json/ext/parser/parser.rl +++ b/ext/json/ext/parser/parser.rl @@ -698,6 +698,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self) json->object_class = Qnil; json->array_class = Qnil; } + source = rb_convert_type(source, T_STRING, "String", "to_str"); if (!json->quirks_mode) { source = convert_encoding(StringValue(source)); } diff --git a/lib/json/common.rb b/lib/json/common.rb index 43e249c..e0d55a0 100644 --- a/lib/json/common.rb +++ b/lib/json/common.rb @@ -296,7 +296,7 @@ module JSON source = source.to_str elsif source.respond_to? :to_io source = source.to_io.read - else + elsif source.respond_to?(:read) source = source.read end result = parse(source, :max_nesting => false, :allow_nan => true) diff --git a/lib/json/pure/parser.rb b/lib/json/pure/parser.rb index e24aac1..84eb67f 100644 --- a/lib/json/pure/parser.rb +++ b/lib/json/pure/parser.rb @@ -73,7 +73,7 @@ module JSON def initialize(source, opts = {}) opts ||= {} unless @quirks_mode = opts[:quirks_mode] - source = determine_encoding source + source = convert_encoding source end super source if !opts.key?(:max_nesting) # defaults to 19 @@ -145,7 +145,12 @@ module JSON private - def determine_encoding(source) + def convert_encoding(source) + if source.respond_to?(:to_str) + source = source.to_str + else + raise TypeError, "#{source.inspect} is not like a string" + end if defined?(::Encoding) if source.encoding == ::Encoding::ASCII_8BIT b = source[0, 4].bytes.to_a diff --git a/tests/test_json.rb b/tests/test_json.rb index eafd758..5492b8e 100755 --- a/tests/test_json.rb +++ b/tests/test_json.rb @@ -107,6 +107,8 @@ class TC_JSON < Test::Unit::TestCase def test_parse_json_primitive_values assert_raise(JSON::ParserError) { JSON.parse('') } assert_raise(JSON::ParserError) { JSON.parse('', :quirks_mode => true) } + assert_raise(TypeError) { JSON.parse(nil) } + assert_raise(TypeError) { JSON.parse(nil, :quirks_mode => true) } assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') } assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ', :quirks_mode => true) } parser = JSON::Parser.new('null') diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb index da96603..2110eba 100755 --- a/tests/test_json_generate.rb +++ b/tests/test_json_generate.rb @@ -210,4 +210,24 @@ EOT ensure GC.stress = stress end if GC.respond_to?(:stress=) + + def test_broken_bignum # [ruby-core:38867] + pid = fork do + Bignum.class_eval do + def to_s + end + end + begin + JSON::Ext::Generator::State.new.generate(1<<64) + exit 1 + rescue TypeError + exit 0 + end + end + _, status = Process.waitpid2(pid) + assert status.success? + rescue NotImplementedError + # forking to avoid modifying core class of a parent process and + # introducing race conditions of tests are run in parallel + end if defined?(JSON::Ext) end |