Skip to content

Commit 2f57f40

Browse files
committed
Raise parse error on invalid comments
1 parent f8817fe commit 2f57f40

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

ext/json/ext/parser/parser.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static const bool whitespace[256] = {
476476
['/'] = 1,
477477
};
478478

479-
static bool
479+
static void
480480
json_eat_comments(JSON_ParserState *state)
481481
{
482482
if (state->cursor + 1 < state->end) {
@@ -496,7 +496,7 @@ json_eat_comments(JSON_ParserState *state)
496496
state->cursor = memchr(state->cursor, '*', state->end - state->cursor);
497497
if (!state->cursor) {
498498
state->cursor = state->end;
499-
break;
499+
raise_parse_error("unexpected end of input, expected closing '*/'", state->cursor);
500500
} else {
501501
state->cursor++;
502502
if (state->cursor < state->end && *state->cursor == '/') {
@@ -508,10 +508,12 @@ json_eat_comments(JSON_ParserState *state)
508508
break;
509509
}
510510
default:
511-
return false;
511+
raise_parse_error("unexpected token at '%s'", state->cursor);
512+
break;
512513
}
514+
} else {
515+
raise_parse_error("unexpected token at '%s'", state->cursor);
513516
}
514-
return true;
515517
}
516518

517519
static inline void
@@ -521,9 +523,7 @@ json_eat_whitespace(JSON_ParserState *state)
521523
if (RB_LIKELY(*state->cursor != '/')) {
522524
state->cursor++;
523525
} else {
524-
if (!json_eat_comments(state)) {
525-
return;
526-
}
526+
json_eat_comments(state);
527527
}
528528
}
529529
}

test/json/json_parser_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ def test_parse_comments
398398
}
399399
JSON
400400
assert_equal({ "key1" => "value1" }, parse(json))
401+
assert_equal({}, parse('{} /**/'))
402+
assert_raise(ParserError) { parse('{} /* comment not closed') }
403+
assert_raise(ParserError) { parse('{} /*/') }
404+
assert_raise(ParserError) { parse('{} /x wrong comment') }
405+
assert_raise(ParserError) { parse('{} /') }
401406
end
402407

403408
def test_nesting

0 commit comments

Comments
 (0)