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

Always dup argument to preserve original encoding for force_encoding #537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1780,9 +1780,7 @@ static VALUE convert_encoding(VALUE source)
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *enc = rb_enc_get(source);
if (enc == rb_ascii8bit_encoding()) {
if (OBJ_FROZEN(source)) {
source = rb_str_dup(source);
}
source = rb_str_dup(source);
FORCE_UTF8(source);
} else {
source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
Expand Down Expand Up @@ -1916,15 +1914,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}


#line 1920 "parser.c"
#line 1918 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};

enum {JSON_en_main = 1};


#line 828 "parser.rl"
#line 826 "parser.rl"


/*
Expand All @@ -1942,16 +1940,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;


#line 1946 "parser.c"
#line 1944 "parser.c"
{
cs = JSON_start;
}

#line 845 "parser.rl"
#line 843 "parser.rl"
p = json->source;
pe = p + json->len;

#line 1955 "parser.c"
#line 1953 "parser.c"
{
if ( p == pe )
goto _test_eof;
Expand Down Expand Up @@ -1985,7 +1983,7 @@ case 1:
cs = 0;
goto _out;
tr2:
#line 820 "parser.rl"
#line 818 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
Expand All @@ -1995,7 +1993,7 @@ cs = 0;
if ( ++p == pe )
goto _test_eof10;
case 10:
#line 1999 "parser.c"
#line 1997 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
Expand Down Expand Up @@ -2084,7 +2082,7 @@ case 9:
_out: {}
}

#line 848 "parser.rl"
#line 846 "parser.rl"

if (cs >= JSON_first_final && p == pe) {
return result;
Expand Down
4 changes: 1 addition & 3 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -675,9 +675,7 @@ static VALUE convert_encoding(VALUE source)
#ifdef HAVE_RUBY_ENCODING_H
rb_encoding *enc = rb_enc_get(source);
if (enc == rb_ascii8bit_encoding()) {
if (OBJ_FROZEN(source)) {
source = rb_str_dup(source);
}
source = rb_str_dup(source);
FORCE_UTF8(source);
} else {
source = rb_str_conv_enc(source, rb_enc_get(source), rb_utf8_encoding());
Expand Down
6 changes: 6 additions & 0 deletions tests/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def test_argument_encoding
assert_equal Encoding::UTF_16, source.encoding
end if defined?(Encoding::UTF_16)

def test_argument_encoding_for_binary
source = "{}".encode("ASCII-8BIT")
JSON::Parser.new(source)
assert_equal Encoding::ASCII_8BIT, source.encoding
end if defined?(Encoding::ASCII_8BIT)

def test_error_message_encoding
bug10705 = '[ruby-core:67386] [Bug #10705]'
json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8)
Expand Down