Skip to content

parse: don't dereference the end of the token vector in a media list - #42

Open
metan-ucw wants to merge 1 commit into
netsurf-browser:masterfrom
metan-ucw:fix-crash-in-media
Open

parse: don't dereference the end of the token vector in a media list#42
metan-ucw wants to merge 1 commit into
netsurf-browser:masterfrom
metan-ucw:fix-crash-in-media

Conversation

@metan-ucw

Copy link
Copy Markdown
Contributor

css__mq_parse_media_list() goes round the loop again after a trailing comma, so mq_parse_media_query() can be entered with the vector already exhausted. Its first peek then returns NULL, and while tokenIsChar() tolerates that, the else if beside it reads token->type straight off the NULL.

A stylesheet ending in an unterminated @media), is enough: the ) fails the first query, the , sends the list round once more, and there is nothing left to parse. Guard the branch and fall through to the iterate below, which reports CSS_INVALID — i.e. not all, the error handling MQ4 3.2 asks for.

The three cases added to mq.dat all segfaulted before this, and differ in how the first query ends, which is what decides whether the list loops at all — @media, and @media not, do not reach the bug because that query consumes the comma itself:

@media), the query fails on the ')', leaving the comma
@media ),,, several empty iterations in a row
@media (color), the first query is a condition, and succeeds

All three now parse to not all (the third keeps its condition, so it dumps as all).

css__mq_parse_media_list() goes round the loop again after a trailing
comma, so mq_parse_media_query() can be entered with the vector already
exhausted.  Its first peek then returns NULL, and while tokenIsChar()
tolerates that, the `else if` beside it reads token->type straight off
the NULL.

A stylesheet ending in an unterminated `@media),` is enough: the `)`
fails the first query, the `,` sends the list round once more, and there
is nothing left to parse.  Guard the branch and fall through to the
iterate below, which reports CSS_INVALID — i.e. `not all`, the error
handling MQ4 3.2 asks for.

The three cases added to mq.dat all segfaulted before this, and differ in
how the first query ends, which is what decides whether the list loops at
all — `@media,` and `@media not,` do not reach the bug because that query
consumes the comma itself:

  @media),         the query fails on the ')', leaving the comma
  @media ),,,      several empty iterations in a row
  @media (color),  the first query is a condition, and succeeds

All three now parse to `not all` (the third keeps its condition, so it
dumps as `all`).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Signed-off-by: Cyril Hrubis <metan@ucw.cz>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant