Skip to content

sequence: drop the anchor that made declaration parsing quadratic - #285

Draft
rnva wants to merge 1 commit into
Ericsson:summerfrom
rnva:fix/participant-declaration-redos
Draft

rnva wants to merge 1 commit into
Ericsson:summerfrom
rnva:fix/participant-declaration-redos

Conversation

@rnva

@rnva rnva commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

PARTICIPANT_DECLARATION_RE ended in an anchored (?P<rest>.*)$. On the single lines the pattern is fed the anchor changed no result, since greedy .* already runs to the end, but it gave the pattern a tail that can fail: a newline in the name made .*$ fail, and the engine then retried the tail at every position it could backtrack [^\s#]+ to -- quadratic in the name's length (~0.4s at 8000 characters, against 0.08ms without the anchor).

Dropping the anchor makes the tail succeed on first try, so the name repetition is never backtracked into. Behaviour is unchanged: the two patterns return identical groupdicts across 108 declaration forms (quoted and bare names, aliases, order/color/stereotype modifiers, indentation, unterminated quotes and non-declarations).

No caller could reach the quadratic path, since participant_declarations and rename.py both split the puml into lines first, so this hardens the pattern rather than fixing a live defect. A timing test pins the linear bound so the anchor is not reinstated.

Reported by CodeQL as py/polynomial-redos. Note for future triage that the alert's suggested input ('participant !' with many '!!') is not in fact slow -- it matches in linear time; the newline is what triggers the backtracking.

PARTICIPANT_DECLARATION_RE ended in an anchored `(?P<rest>.*)$`. On the
single lines the pattern is fed the anchor changed no result, since greedy
`.*` already runs to the end, but it gave the pattern a tail that can fail:
a newline in the name made `.*$` fail, and the engine then retried the tail
at every position it could backtrack `[^\s#]+` to -- quadratic in the name's
length (~0.4s at 8000 characters, against 0.08ms without the anchor).

Dropping the anchor makes the tail succeed on first try, so the name
repetition is never backtracked into. Behaviour is unchanged: the two
patterns return identical groupdicts across 108 declaration forms (quoted
and bare names, aliases, order/color/stereotype modifiers, indentation,
unterminated quotes and non-declarations).

No caller could reach the quadratic path, since participant_declarations
and rename.py both split the puml into lines first, so this hardens the
pattern rather than fixing a live defect. A timing test pins the linear
bound so the anchor is not reinstated.

Reported by CodeQL as py/polynomial-redos. Note for future triage that the
alert's suggested input ('participant !' with many '!!') is not in fact
slow -- it matches in linear time; the newline is what triggers the
backtracking.
@rnva
rnva marked this pull request as draft October 8, 2026 18:41
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