Skip to content

Commit

Permalink
Merge pull request #537 from aeroastro/feature/stop-changing-original…
Browse files Browse the repository at this point in the history
…-encoding

Always dup argument to preserve original encoding for force_encoding
  • Loading branch information
byroot authored Oct 17, 2024
2 parents 7106b03 + db9a489 commit e272c89
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 93 deletions.
21 changes: 9 additions & 12 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1778,10 +1778,7 @@ static VALUE convert_encoding(VALUE source)
}

if (encindex == binary_encindex) {
if (OBJ_FROZEN(source)) {
source = rb_str_dup(source);
}
return rb_enc_associate_index(source, utf8_encindex);
return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
}

return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding());
Expand Down Expand Up @@ -1912,15 +1909,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}


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

enum {JSON_en_main = 1};


#line 824 "parser.rl"
#line 821 "parser.rl"


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


#line 1942 "parser.c"
#line 1939 "parser.c"
{
cs = JSON_start;
}

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

#line 1951 "parser.c"
#line 1948 "parser.c"
{
if ( p == pe )
goto _test_eof;
Expand Down Expand Up @@ -1981,7 +1978,7 @@ case 1:
cs = 0;
goto _out;
tr2:
#line 816 "parser.rl"
#line 813 "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 @@ -1991,7 +1988,7 @@ cs = 0;
if ( ++p == pe )
goto _test_eof10;
case 10:
#line 1995 "parser.c"
#line 1992 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
Expand Down Expand Up @@ -2080,7 +2077,7 @@ case 9:
_out: {}
}

#line 844 "parser.rl"
#line 841 "parser.rl"

if (cs >= JSON_first_final && p == pe) {
return result;
Expand Down
5 changes: 1 addition & 4 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,7 @@ static VALUE convert_encoding(VALUE source)
}

if (encindex == binary_encindex) {
if (OBJ_FROZEN(source)) {
source = rb_str_dup(source);
}
return rb_enc_associate_index(source, utf8_encindex);
return rb_enc_associate_index(rb_str_dup(source), utf8_encindex);
}

return rb_str_conv_enc(source, rb_enc_from_index(encindex), rb_utf8_encoding());
Expand Down
Loading

0 comments on commit e272c89

Please sign in to comment.