Skip to content

Null access for empty matches with greedy matching #3052

@RunDevelopment

Description

@RunDevelopment

So I just found this bug. If a greedy token creates an empty match m at the end of the string (m = re.exec(text); m[0] == "" && m.index == text.length), then matchGrammar will throw a TypeError. This line throws the error.

What happens is that the greedy matching algorithm tries to find the string or token with the index of m. Since there cannot be an empty string or token at the end of the token stream, we will instead try to access the string value of the tail node of the linked list. That tail node has a value of null, boom, TypeError.

Example:

Prism.languages.test = {
  'oh-no': {
    pattern: /$/,
    greedy: true
  }
}

Prism.highlight('foo', Prism.languages.test, 'test');

image


This is not a high prior bug. This bug has been in Prism ever since greedy matching was introduced and nobody noticed up until now.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions