Repository navigation
Fix two cases of quadratic behavior (GHSA-66g8-4hjf-77xh) - #520
Merged
Merged
Conversation
If we find consecutive blank lines inside a list item, abort early in check_open_blocks by returning NULL. This makes S_process_line skip the calls to open_new_blocks and add_text_to_container. open_new_blocks is a no-op for blank lines. add_text_to_container would add an empty line only for code and HTML blocks which we account for in check_open_blocks. Fixes part of GHSA-66g8-4hjf-77xh. Obsoletes commonmark#475.
Don't use public tree manipulation API. The cycle detection can result in quadratic behavior in deeply nested trees. Manipulate the tree structure manually which is error-prone but also more efficient. Fixes part of GHSA-66g8-4hjf-77xh.
Member
|
This gave me a pretty significant speedup on our regular |
Contributor
Author
|
The speedup is probably caused by the change to |
Member
|
Yes - saw that! |
notriddle
added a commit
to pulldown-cmark/pulldown-cmark
that referenced
this pull request
Sep 25, 2026
This commit accomplishes the same thing as <#1145>, but without allocating more memory. The fix is shamelessly "borrowed" from <commonmark/cmark#520>, which fixes the same bug. Deeply nested lists followed by blank lines parsed in quadratic time. `scan_containers` runs once per line and walked the whole spine, and on a blank line every list item matched without consuming anything, so each blank line cost time proportional to the nesting depth. List levels cost two bytes each and blank lines one, so a small input goes a long way. Before ------ ```console % python3 -c 'N=800; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 16% cpu 0.076 total % python3 -c 'N=8_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.34s user 0.00s system 88% cpu 0.390 total % python3 -c 'N=80_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 34.42s user 0.18s system 99% cpu 34.770 total % python3 -c 'N=800; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.00s system 15% cpu 0.073 total % python3 -c 'N=8_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.34s user 0.00s system 87% cpu 0.386 total % python3 -c 'N=80_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 33.39s user 0.09s system 99% cpu 33.562 total ``` After ----- ```console % python3 -c 'N=800; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 7% cpu 0.113 total % python3 -c 'N=8_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 11% cpu 0.077 total % python3 -c 'N=80_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 21% cpu 0.087 total % python3 -c 'N=800; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.00s system 7% cpu 0.075 total % python3 -c 'N=8_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 13% cpu 0.075 total % python3 -c 'N=80_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 28% cpu 0.067 total ```
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.
Fixes the two issues reported here: GHSA-66g8-4hjf-77xh
Also known as CVE-2023-24824.