Skip to content

Quadratic behavior when parsing smart quotes #521

@nwellnhof

Description

@nwellnhof

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions