Skip to content

Commit c737292

Browse files
committed
Prevent ResumableParser double initialization
Reported-By: Frank Kuhn <frank@swipergy.com>
1 parent 3f91317 commit c737292

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

‎ext/json/ext/parser/parser.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,7 @@ typedef struct JSON_ResumableParserStruct {
22612261
size_t incomplete_bytes;
22622262
bool complete;
22632263
bool in_use;
2264+
bool initialized;
22642265
} JSON_ResumableParser;
22652266

22662267
static void JSON_ResumableParser_mark(void *ptr)
@@ -2397,9 +2398,15 @@ static VALUE cResumableParser_initialize(int argc, VALUE *argv, VALUE self)
23972398
{
23982399
rb_check_frozen(self);
23992400

2401+
JSON_ResumableParser *parser = cResumableParser_get(self);
2402+
2403+
if (parser->initialized) {
2404+
rb_raise(rb_eTypeError, "already initialized parser");
2405+
}
2406+
parser->initialized = true;
2407+
24002408
VALUE opts = Qfalse;
24012409
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "0:", &opts);
2402-
JSON_ResumableParser *parser = cResumableParser_get(self);
24032410

24042411
opts = argc > 0 ? argv[0] : Qnil;
24052412
parser_config_init(&parser->config, opts, self, true);

‎test/json/resumable_parser_test.rb‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@ def test_parse_error_message
717717
assert_equal 0, error.column
718718
end
719719

720+
def test_prevent_double_initialize
721+
assert_raise TypeError do
722+
@parser.send(:initialize)
723+
end
724+
end
725+
720726
private
721727

722728
def assert_parse_error(json, expected_error_message = nil)

0 commit comments

Comments
 (0)