commonmark / commonmark/cmark

Quadratic behavior when parsing smart quotes

Open
#521 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
2k
Forks
691
Avg merge
1d 16h
Merged PRs (30d)
1

Description

Reproduce with:

python3 -c 'print("\x27x " * 30000 + "\x27\"x" * 30000)' |\
    time build/src/cmark --smart >/dev/null

After unquoting, this input looks like

'x 'x 'x 'x 'x '"x'"x'"x'"x'"x

This also works with emphasis:

python3 -c 'print("\"x " * 30000 + "\"_x" * 30000)' |\
    time build/src/cmark --smart >/dev/null

When matching a pair of smart quotes, we currently don't remove delimiters (emphasis or other smart quotes) between the quotes. This can lead to quadratic behavior when processing deeply nested quotes interspersed with emphasis or different quotes.

A simple way to fix this issue is to delete delimiters between smart quotes but this changes the parsing rules. Another, more complicated approach is to handle single quotes, double quotes and emphasis in separate lists.

This is the only remaining issue with quadratic complexity in the parser that I could find with a specialized fuzzer similar to the "quadratic" fuzzers in cmark-gfm. Note that there are still some issues in the serializers.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the command against build/src/cmark and inspect the parser path for smart-quote and emphasis delimiter matching. Compare the behavior with the quadratic fuzzers in cmark-gfm, then determine which parsing approach preserves the intended rules while avoiding repeated work. Done means the reported inputs no longer show quadratic behavior and the relevant parsing behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, markdown
Domain
cli, compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.