Skip to content

Commit

Permalink
Raise parse error on invalid comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tompng committed Jan 20, 2025
1 parent f8817fe commit 2f57f40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 7 additions & 7 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ static const bool whitespace[256] = {
['/'] = 1,
};

static bool
static void
json_eat_comments(JSON_ParserState *state)
{
if (state->cursor + 1 < state->end) {
Expand All @@ -496,7 +496,7 @@ json_eat_comments(JSON_ParserState *state)
state->cursor = memchr(state->cursor, '*', state->end - state->cursor);
if (!state->cursor) {
state->cursor = state->end;
break;
raise_parse_error("unexpected end of input, expected closing '*/'", state->cursor);
} else {
state->cursor++;
if (state->cursor < state->end && *state->cursor == '/') {
Expand All @@ -508,10 +508,12 @@ json_eat_comments(JSON_ParserState *state)
break;
}
default:
return false;
raise_parse_error("unexpected token at '%s'", state->cursor);
break;
}
} else {
raise_parse_error("unexpected token at '%s'", state->cursor);
}
return true;
}

static inline void
Expand All @@ -521,9 +523,7 @@ json_eat_whitespace(JSON_ParserState *state)
if (RB_LIKELY(*state->cursor != '/')) {
state->cursor++;
} else {
if (!json_eat_comments(state)) {
return;
}
json_eat_comments(state);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ def test_parse_comments
}
JSON
assert_equal({ "key1" => "value1" }, parse(json))
assert_equal({}, parse('{} /**/'))
assert_raise(ParserError) { parse('{} /* comment not closed') }
assert_raise(ParserError) { parse('{} /*/') }
assert_raise(ParserError) { parse('{} /x wrong comment') }
assert_raise(ParserError) { parse('{} /') }
end

def test_nesting
Expand Down

0 comments on commit 2f57f40

Please sign in to comment.