Skip to content

Commit 96079a1

Browse files
authored
Merge pull request #734 from tompng/error_on_invalid_comments
Raise parse error on invalid comments
2 parents 5934182 + 2f57f40 commit 96079a1

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
@@ -406,6 +406,11 @@ def test_parse_comments
406406
}
407407
JSON
408408
assert_equal({ "key1" => "value1" }, parse(json))
409+
assert_equal({}, parse('{} /**/'))
410+
assert_raise(ParserError) { parse('{} /* comment not closed') }
411+
assert_raise(ParserError) { parse('{} /*/') }
412+
assert_raise(ParserError) { parse('{} /x wrong comment') }
413+
assert_raise(ParserError) { parse('{} /') }
409414
end
410415

411416
def test_nesting

0 commit comments

Comments
 (0)