summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2016-04-24 02:04:34 +0900
committerFlorian Frank <flori@ping.de>2016-06-21 10:56:58 +0200
commit2d475160b490389060d66980465bb7220b2363e7 (patch)
tree0f1a117cbe37be130deebe3256ac9ed5669f55c5
parent0a49b1f7d5bccfa324cdc74ba6f899f3925a7830 (diff)
downloadjson-2d475160b490389060d66980465bb7220b2363e7.tar.gz
Exception encoding
Raise with messages in UTF-8 encoding.
-rw-r--r--ext/json/ext/parser/parser.c34
-rw-r--r--ext/json/ext/parser/parser.rl2
-rw-r--r--tests/json_parser_test.rb10
3 files changed, 44 insertions, 2 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 269c9ca..a99b73b 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -1810,7 +1810,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 1814 "parser.c"
+=======
+#line 1801 "parser.c"
+>>>>>>> Exception encoding
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1818,7 +1822,11 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 722 "parser.rl"
+=======
+#line 709 "parser.rl"
+>>>>>>> Exception encoding
/*
@@ -1835,16 +1843,28 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 1839 "parser.c"
+=======
+#line 1826 "parser.c"
+>>>>>>> Exception encoding
{
cs = JSON_start;
}
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 738 "parser.rl"
p = json->source;
pe = p + json->len;
#line 1848 "parser.c"
+=======
+#line 725 "parser.rl"
+ p = json->source;
+ pe = p + json->len;
+
+#line 1835 "parser.c"
+>>>>>>> Exception encoding
{
if ( p == pe )
goto _test_eof;
@@ -1878,7 +1898,11 @@ st0:
cs = 0;
goto _out;
tr2:
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 714 "parser.rl"
+=======
+#line 701 "parser.rl"
+>>>>>>> Exception encoding
{
char *np = JSON_parse_value(json, p, pe, &result);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1888,7 +1912,11 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 1892 "parser.c"
+=======
+#line 1879 "parser.c"
+>>>>>>> Exception encoding
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1977,12 +2005,16 @@ case 9:
_out: {}
}
+<<<<<<< 2e06c964fceb550203659db23971459bc9d35054
#line 741 "parser.rl"
+=======
+#line 728 "parser.rl"
+>>>>>>> Exception encoding
if (cs >= JSON_first_final && p == pe) {
return result;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
return Qnil;
}
}
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index 0c65a09..eac3e6c 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -742,7 +742,7 @@ static VALUE cParser_parse(VALUE self)
if (cs >= JSON_first_final && p == pe) {
return result;
} else {
- rb_raise(eParserError, "%u: unexpected token at '%s'", __LINE__, p);
+ rb_enc_raise(EXC_ENCODING eParserError, "%u: unexpected token at '%s'", __LINE__, p);
return Qnil;
}
}
diff --git a/tests/json_parser_test.rb b/tests/json_parser_test.rb
index d2b0fd1..1d1987b 100644
--- a/tests/json_parser_test.rb
+++ b/tests/json_parser_test.rb
@@ -19,6 +19,16 @@ class JSONParserTest < Test::Unit::TestCase
assert_equal Encoding::UTF_16, source.encoding
end if defined?(Encoding::UTF_16)
+ def test_error_message_encoding
+ bug10705 = '[ruby-core:67386] [Bug #10705]'
+ json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8)
+ e = assert_raise(JSON::ParserError) {
+ JSON::Ext::Parser.new(json).parse
+ }
+ assert_equal(Encoding::UTF_8, e.message.encoding, bug10705)
+ assert_include(e.message, json, bug10705)
+ end if defined?(Encoding::UTF_8) and defined?(JSON::Ext::Parser)
+
def test_parsing
parser = JSON::Parser.new('"test"')
assert_equal 'test', parser.parse