Skip to content

Fix quadratic parsing of a line of *a_ - #1155

Merged
Martin1887 merged 3 commits into
pulldown-cmark:mainfrom
HardMax71:underscore-lower-bound
Sep 29, 2026
Merged

Martin1887 merged 3 commits into
pulldown-cmark:mainfrom
HardMax71:underscore-lower-bound

Conversation

@HardMax71

@HardMax71 HardMax71 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1154.

set_lowerbound raised only UNDERSCORE_NOT_BOTH for an underscore that can close but not open, while get_lowerbound for such a closer takes the min of that and the mod-3 slot, which stayed at 0. This raises both, the way asterisks already do. It's safe because a closer that can't open matches every opener that a two-way closer of the same length would match, so when its search fails, the two-way closer's search would fail over the same range too.

A 1.2 MB line of *a_ took 26.8 s on main and takes 0.07 s here. The mirrored _a* was already linear.

issue_1154 in parse.rs pushes three * openers, lets a closing-only _ fail, and checks that both underscore bounds end up at 3. On main the closing-only bound stays at 0. The dos-fuzzer regressions now check *_ too. *a_ itself scores as linear there, because the fuzzer turns on CJK-friendly emphasis, where a * right after _ can also close. *_ keeps the * opening-only and the _ closing-only in both modes, and --regressions failed 9 of 10 runs on main and passed 10 of 10 here.

This should change no output. The full suite passes. I also compared main and this branch on 300,000 random short paragraphs of emphasis, strikethrough, super/subscript and highlight delimiters, with and without those extensions enabled, plus 400,000 underscore-heavy ones. The HTML came out byte-for-byte identical.

Copilot AI lite review requested due to automatic review settings September 26, 2026 21:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@notriddle

Copy link
Copy Markdown
Collaborator

Please add a test case to

fn regression_test() -> i32 {

@HardMax71

HardMax71 commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@notriddle Added *_ to the regressions list. *a_ itself scores as linear there, because the fuzzer turns on CJK-friendly emphasis, where a * right after _ can also close. With *_ , --regressions failed 9 of 10 runs on main and passed 10 of 10 on this branch.

A `_` that can close but not open and found no opener raised only UNDERSCORE_NOT_BOTH, while get_lowerbound for the next one takes the min of that and the still-zero mod-3 slot, so each such closer rescanned the whole stack. Raise both, as asterisks already do. A line of `*a_` went quadratic: 1.2 MB took 26.8 s, now 0.07 s. Fixes #1154.
The fuzzer parses with every option on, and under CJK-friendly emphasis a `*` right after `_` can also close, so `*a_` stays linear there. `*_ ` keeps the `*` opening-only and the `_` closing-only in both modes. With it, `--regressions` failed 9 of 10 runs on main and passed 10 of 10 with the fix.
@HardMax71
HardMax71 force-pushed the underscore-lower-bound branch from 4d9b61d to bb174f0 Compare September 28, 2026 18:17
Comment thread pulldown-cmark/src/parse.rs Resolved
@Martin1887
Martin1887 merged commit 3baf748 into pulldown-cmark:main Sep 29, 2026
6 checks passed
@notriddle notriddle mentioned this pull request Oct 20, 2026
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.

Quadratic time on a line of *a_

4 participants