Skip to content

Fix subtract-overflow panic in handle_wikilink on malformed input - #1111

Merged
Martin1887 merged 2 commits into
pulldown-cmark:mainfrom
teddytennant:fix-wikilink-overflow
Jul 8, 2026
Merged

Martin1887 merged 2 commits into
pulldown-cmark:mainfrom
teddytennant:fix-wikilink-overflow

Conversation

@teddytennant

Copy link
Copy Markdown
Contributor

FirstPass::handle_wikilink computes start_ix (the wikilink body node's start) and end_ix (the current node's start), then uses end_ix - start_ix as a length and slices &block_text[start_ix..end_ix]. On some malformed image/wikilink inputs the body node begins at or after the closing delimiter, so start_ix > end_ix: the subtraction underflows and panics (attempt to subtract with overflow in debug builds), and the slice range is inverted.

This adds an early bail-out — matching the function's documented "bail early in case the link is malformed" behavior and its existing return None; idiom — right after both indices are computed:

if end_ix <= start_ix {
    return None;
}

Test

Added a regression test that runs the reproducer from the issue (" \u{fffd} ![[] ]()]]") through Parser::new_ext(.., Options::all()) and html::push_html, asserting it no longer panics. Without the guard this input panics with the subtraction overflow; with it, the full test suite (1212 tests) passes.

Closes #1108.

Copilot AI review requested due to automatic review settings July 8, 2026 00:08

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.

@Martin1887

Copy link
Copy Markdown
Collaborator

Thanks! Please write the test in the form of the other regression tests in specs/regression.txt.

Per review, replace the hand-written unit test with a regression spec
entry (issue pulldown-cmark#1108) so it runs through the standard test harness.
@teddytennant

Copy link
Copy Markdown
Contributor Author

Done — moved it into specs/regression.txt as a wikilinks regression example (with the minimal repro ![[] ]()]]), matching the other entries, and dropped the standalone unit test. Regenerated tests/suite/regression.rs and confirmed the suite passes.

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.

Abort with input � ![[] ]()]]

3 participants