* ext/json/parser/parser.h (GET_PARSER): check if initialized.

[ruby-core:35079]
* ext/json/parser/parser.rl (cParser_initialize): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30791 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2011-02-05 01:30:01 +00:00
Родитель bc1e4b4e2a
Коммит 9cc62abc6b
5 изменённых файлов: 34 добавлений и 7 удалений

Просмотреть файл

@ -1,3 +1,10 @@
Sat Feb 5 10:29:52 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* ext/json/parser/parser.h (GET_PARSER): check if initialized.
[ruby-core:35079]
* ext/json/parser/parser.rl (cParser_initialize): ditto.
Sat Feb 5 10:09:31 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Feb 5 10:09:31 2011 Nobuyoshi Nakada <nobu@ruby-lang.org>
* load.c (rb_get_expanded_load_path): always expand load paths. * load.c (rb_get_expanded_load_path): always expand load paths.

Просмотреть файл

@ -1610,7 +1610,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
char *ptr; char *ptr;
long len; long len;
VALUE source, opts; VALUE source, opts;
GET_PARSER; GET_PARSER_INIT;
if (json->Vsource) {
rb_raise(rb_eArgError, "already initialized instance");
}
rb_scan_args(argc, argv, "11", &source, &opts); rb_scan_args(argc, argv, "11", &source, &opts);
source = convert_encoding(StringValue(source)); source = convert_encoding(StringValue(source));
ptr = RSTRING_PTR(source); ptr = RSTRING_PTR(source);
@ -1698,16 +1702,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER; GET_PARSER;
#line 1702 "parser.c" #line 1706 "parser.c"
{ {
cs = JSON_start; cs = JSON_start;
} }
#line 699 "parser.rl" #line 703 "parser.rl"
p = json->source; p = json->source;
pe = p + json->len; pe = p + json->len;
#line 1711 "parser.c" #line 1715 "parser.c"
{ {
if ( p == pe ) if ( p == pe )
goto _test_eof; goto _test_eof;
@ -1784,7 +1788,7 @@ st10:
if ( ++p == pe ) if ( ++p == pe )
goto _test_eof10; goto _test_eof10;
case 10: case 10:
#line 1788 "parser.c" #line 1792 "parser.c"
switch( (*p) ) { switch( (*p) ) {
case 13: goto st10; case 13: goto st10;
case 32: goto st10; case 32: goto st10;
@ -1841,7 +1845,7 @@ case 9:
_out: {} _out: {}
} }
#line 702 "parser.rl" #line 706 "parser.rl"
if (cs >= JSON_first_final && p == pe) { if (cs >= JSON_first_final && p == pe) {
return result; return result;
@ -1938,5 +1942,6 @@ void Init_parser()
* Local variables: * Local variables:
* mode: c * mode: c
* c-file-style: ruby * c-file-style: ruby
* indent-tabs-mode: nil
* End: * End:
*/ */

Просмотреть файл

@ -44,6 +44,9 @@ typedef struct JSON_ParserStruct {
} JSON_Parser; } JSON_Parser;
#define GET_PARSER \ #define GET_PARSER \
GET_PARSER_INIT; \
if (!json->Vsource) rb_raise(rb_eArgError, "uninitialized instance")
#define GET_PARSER_INIT \
JSON_Parser *json; \ JSON_Parser *json; \
Data_Get_Struct(self, JSON_Parser, json) Data_Get_Struct(self, JSON_Parser, json)

Просмотреть файл

@ -608,7 +608,11 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
char *ptr; char *ptr;
long len; long len;
VALUE source, opts; VALUE source, opts;
GET_PARSER; GET_PARSER_INIT;
if (json->Vsource) {
rb_raise(rb_eArgError, "already initialized instance");
}
rb_scan_args(argc, argv, "11", &source, &opts); rb_scan_args(argc, argv, "11", &source, &opts);
source = convert_encoding(StringValue(source)); source = convert_encoding(StringValue(source));
ptr = RSTRING_PTR(source); ptr = RSTRING_PTR(source);
@ -795,5 +799,6 @@ void Init_parser()
* Local variables: * Local variables:
* mode: c * mode: c
* c-file-style: ruby * c-file-style: ruby
* indent-tabs-mode: nil
* End: * End:
*/ */

Просмотреть файл

@ -391,4 +391,11 @@ EOT
json5 = JSON([orig = 1 << 64]) json5 = JSON([orig = 1 << 64])
assert_equal orig, JSON[json5][0] assert_equal orig, JSON[json5][0]
end end
def test_allocate
json = JSON::Parser.new("{}")
assert_raises(ArgumentError, '[ruby-core:35079]') {json.__send__(:initialize, "{}")}
json = JSON::Parser.allocate
assert_raises(ArgumentError, '[ruby-core:35079]') {json.source}
end
end end