Skip to content

Commit

Permalink
Fix issue ruby#296 when parsing frozen strings
Browse files Browse the repository at this point in the history
  • Loading branch information
flori committed Jul 26, 2016
1 parent bb5b677 commit 047814c
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 84 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.1
2.0.2
19 changes: 11 additions & 8 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1670,6 +1670,9 @@ 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);
}
FORCE_UTF8(source);
} else {
source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
Expand Down Expand Up @@ -1805,15 +1808,15 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}


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

enum {JSON_en_main = 1};


#line 717 "parser.rl"
#line 720 "parser.rl"


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


#line 1834 "parser.c"
#line 1837 "parser.c"
{
cs = JSON_start;
}

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

#line 1843 "parser.c"
#line 1846 "parser.c"
{
if ( p == pe )
goto _test_eof;
Expand Down Expand Up @@ -1873,7 +1876,7 @@ case 1:
cs = 0;
goto _out;
tr2:
#line 709 "parser.rl"
#line 712 "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 @@ -1883,7 +1886,7 @@ cs = 0;
if ( ++p == pe )
goto _test_eof10;
case 10:
#line 1887 "parser.c"
#line 1890 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
Expand Down Expand Up @@ -1972,7 +1975,7 @@ case 9:
_out: {}
}

#line 736 "parser.rl"
#line 739 "parser.rl"

if (cs >= JSON_first_final && p == pe) {
return result;
Expand Down
3 changes: 3 additions & 0 deletions ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ 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);
}
FORCE_UTF8(source);
} else {
source = rb_str_conv_enc(source, NULL, rb_utf8_encoding());
Expand Down
Loading

0 comments on commit 047814c

Please sign in to comment.