Skip to content

Commit

Permalink
Fix a regression in the parser with leading /
Browse files Browse the repository at this point in the history
Ref: ruby/ruby#12598

This could lead to an infinite loop.
  • Loading branch information
byroot committed Jan 20, 2025
1 parent d4e5284 commit f8cfa26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 6 additions & 3 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 void
static bool
json_eat_comments(JSON_ParserState *state)
{
if (state->cursor + 1 < state->end) {
Expand Down Expand Up @@ -508,9 +508,10 @@ json_eat_comments(JSON_ParserState *state)
break;
}
default:
return;
return false;
}
}
return true;
}

static inline void
Expand All @@ -520,7 +521,9 @@ json_eat_whitespace(JSON_ParserState *state)
if (RB_LIKELY(*state->cursor != '/')) {
state->cursor++;
} else {
json_eat_comments(state);
if (!json_eat_comments(state)) {
return;
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,13 @@ def test_parse_error_incomplete_hash
end
end

def test_parse_leading_slash
# ref: https://github.com/ruby/ruby/pull/12598
assert_raise(JSON::ParserError) do
JSON.parse("/foo/bar")
end
end

private

def string_deduplication_available?
Expand Down

0 comments on commit f8cfa26

Please sign in to comment.