Skip to content

Fix quadratic parsing with a [^ on every line when footnotes are enabled - #1136

Merged
Martin1887 merged 3 commits into
pulldown-cmark:mainfrom
bianyeyu:fix-1076-footnote-quadratic
Sep 25, 2026
Merged

Martin1887 merged 3 commits into
pulldown-cmark:mainfrom
bianyeyu:fix-1076-footnote-quadratic

Conversation

@bianyeyu

Copy link
Copy Markdown

Title

Fix quadratic parsing with a [^ on every line when footnotes are enabled

Body

scan_paragraph_interrupt_no_table probes for a footnote definition on each line when ENABLE_FOOTNOTES is set. For a [^-prefixed line it validated the entire remaining input:

core::str::from_utf8(&bytes[2..]).unwrap()

bytes[2..] is the rest of the document, so a [^ at the start of every line makes this check quadratic (one of the superlinear patterns reported in #1076).

The linebreak handler is &|_| None, so the probe never looks past the current line. Limit the slice to that line:

core::str::from_utf8(&bytes[2..2 + scan_nextline(&bytes[2..])]).unwrap()

On a release build (Apple M4), parsing 80,000 lines of [^x]\n (400 KB) went from ~292 ms to ~9 ms.

Tests added in tests/errors.rs: a 10,000-line smoke test with ENABLE_FOOTNOTES, and a check that a footnote definition still interrupts a paragraph without a preceding blank line.

@notriddle

Copy link
Copy Markdown
Collaborator

I've tested this, and can confirm that it has quadratic behavior on the main branch:

% python3 -c 'N=100000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes  > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null  0.50s user 0.01s system 96% cpu 0.524 total
% python3 -c 'N=1000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes  > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null  49.10s user 0.20s system 99% cpu 49.567 total

And that the PR seems to fix it:

% python3 -c 'N=100000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes  > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null  0.02s user 0.01s system 13% cpu 0.176 total
% python3 -c 'N=1000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes  > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null  0.09s user 0.03s system 85% cpu 0.147 total
% python3 -c 'N=10000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes  > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null  0.91s user 0.43s system 88% cpu 1.505 total

Comment thread pulldown-cmark/tests/errors.rs Outdated
// recognize a definition that interrupts a paragraph (no blank line before it),
// which is exactly the path the #1076 fix touches.
#[test]
fn test_footnote_definition_interrupts_paragraph() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test is redundant. We already have a paragraph interruption test in pulldown-cmark/specs/footnotes.txt.

Comment thread pulldown-cmark/src/firstpass.rs Outdated
// (via `from_utf8(&bytes[2..])`) used to make the UTF-8 validation
// O(n) per line, which is O(n²) overall for inputs with a `[^` on
// every line. Limit the slice to the current line; see #1076.
core::str::from_utf8(&bytes[2..2 + scan_nextline(&bytes[2..])]).unwrap(),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't really like this fix. Couldn't we make scan_paragraph_interrupt_no_table accept a &str instead of a &[u8], and, then, not have to do UTF-8 validation at all?

@bianyeyu

Copy link
Copy Markdown
Author

Dropped the extra test.

scan_paragraph_interrupt_no_table now takes &str, so the footnote probe can use &text[2..] and does not call from_utf8 on the remaining input.

Comment thread pulldown-cmark/src/firstpass.rs Outdated
Comment on lines +2314 to +2317
// Already a `&str`, so this no longer re-validates the remaining
// input as UTF-8 on every `[^` line. The linebreak handler
// rejects newlines, so the probe still only inspects the current
// line; see #1076.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would leave this comment out. It's just a confusing piece of history.

Comment thread pulldown-cmark/tests/errors.rs Outdated
Comment on lines +147 to +158
// Regression for https://github.com/pulldown-cmark/pulldown-cmark/issues/1076
// (superlinear parsing with a `[^` on every line).
#[test]
fn test_footnote_like_lines_no_panic() {
// The original O(n²) trigger: a `[^`-prefixed line repeated. Parsing must
// complete without panicking. Uses ENABLE_FOOTNOTES (GFM), which is the path
// in the issue, not ENABLE_OLD_FOOTNOTES.
let mut opts = Options::empty();
opts.insert(Options::ENABLE_FOOTNOTES);
let input = "[^x]\n".repeat(10_000);
let _ = Parser::new_ext(&input, opts).count();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is also not the right way to test a superlinear time regression (the code never panicked, right?). There's a place in dos-fuzzer for regression tests like this.

The footnote-definition probe in scan_paragraph_interrupt_no_table
validates the entire remaining input for each [^-prefixed line via
from_utf8(&bytes[2..]), making UTF-8 validation O(n) per line and O(n^2)
overall. Limit the slice to the current line, matching the probe's
&|_| None line-break handler.

Fixes pulldown-cmark#1076.
Take &str in scan_paragraph_interrupt_no_table so the [^ probe can
use the existing document string instead of from_utf8 on the remaining
input. Drop the extra interrupt test already covered by footnotes.txt.
Drop the leftover comment. The errors.rs smoke test was not a
superlinear check.
@bianyeyu

Copy link
Copy Markdown
Author

Removed the leftover comment and the errors.rs test.
Added [^x]\n to the dos-fuzzer --regressions list instead.
#1076

@bianyeyu
bianyeyu force-pushed the fix-1076-footnote-quadratic branch from 17eafcc to d8a0044 Compare August 19, 2026 15:28
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.

4 participants