Skip to content

Fix two cases of quadratic behavior (GHSA-66g8-4hjf-77xh) - #520

Merged
jgm merged 3 commits into
commonmark:masterfrom
nwellnhof:quadratic-behavior
Jan 15, 2024
Merged

jgm merged 3 commits into
commonmark:masterfrom
nwellnhof:quadratic-behavior

Conversation

@nwellnhof

Copy link
Copy Markdown
Contributor

Fixes the two issues reported here: GHSA-66g8-4hjf-77xh

Also known as CVE-2023-24824.

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.
@jgm
jgm merged commit 76cbc2d into commonmark:master Jan 15, 2024
@jgm

jgm commented Jan 15, 2024

Copy link
Copy Markdown
Member

This gave me a pretty significant speedup on our regular make bench - from 0.052 to 0.042 s.

@nwellnhof

Copy link
Copy Markdown
Contributor Author

The speedup is probably caused by the change to S_insert_emph.

@jgm

jgm commented Jan 15, 2024

Copy link
Copy Markdown
Member

Yes - saw that!

@nwellnhof
nwellnhof deleted the quadratic-behavior branch March 25, 2024 16:58
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
```
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.

2 participants