fix(c,cpp): quadratic ReDoS in FUNCTION_DECLARATION regex (#4362)#4413
Open
xxiaoxiong wants to merge 2 commits into
Open
fix(c,cpp): quadratic ReDoS in FUNCTION_DECLARATION regex (#4362)#4413xxiaoxiong wants to merge 2 commits into
xxiaoxiong wants to merge 2 commits into
Conversation
…ion with array syntax When TypeScript uses angle bracket type assertions with array syntax (e.g. `<string[]>value`), the XML_TAG pattern in JavaScript grammar was incorrectly matching `<string` as a potential HTML tag start. Since `[` cannot appear immediately after an HTML tag name, this change marks it as a non-HTML pattern, preserving correct highlighting for TypeScript type assertions. Fixes highlightjs#4301
The begin pattern '(' + FUNCTION_TYPE_RE + '[\*&\s]+)+' + FUNCTION_TITLE
contains a nested quantifier: the outer + wraps a group that itself
contains [\*&\s]+. When applied to input without a matching function
title (e.g. repeated words separated by spaces), the regex engine tries
all possible ways to partition whitespace between inner and outer
groups, resulting in O(n^2) backtracking.
Fix by restructuring the pattern to avoid nested quantifiers:
(' + FUNCTION_TYPE_RE + '\s*[\*&]+\s*)*(?:' + FUNCTION_TYPE_RE
+ '\s*[\*&]*\s*)?' + FUNCTION_TITLE
The outer + is split into an outer * (repeating type+modifier pairs)
and a final optional type pair, with whitespace and pointer/reference
operators matched separately in each group rather than in a single
overlapping character class. This eliminates the quadratic backtracking
while preserving the original matching semantics.
Fixes highlightjs#4362
Affects: C (c.js) and C++ (cpp.js), inherited by Arduino.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes a quadratic ReDoS vulnerability in the C/C++ FUNCTION_DECLARATION regex.
Root Cause
The pattern for FUNCTION_DECLARATION contains a nested quantifier:
The outer
+wraps a group whose inner[\*&\s]+also uses+. When applied to input without a matching function title (e.g. repeated words separated by spaces), the regex engine tries all possible ways to partition whitespace between the inner and outer groups, resulting in O(n²) backtracking.Fix
Restructured the pattern to avoid the nested quantifier by splitting the outer
+into an outer*(repeating type+modifier pairs) plus a final optional type pair:PoC
Affected Languages
c.js)cpp.js)Fixes #4362