Skip to content

regexp/syntax: quadratic time for compilation in some cases #39542

@andybalholm

Description

@andybalholm

As discussed on https://groups.google.com/forum/#!topic/golang-nuts/8M-qVbRKIWA

Playground link to illustrate: https://play.golang.org/p/82UBmyfyqV-

Regular expressions with large numbers of empty alternations are taking a long time to compile. The time required is roughly quadratic in the length of the regex.

The quadratic algorithm is building the patchlist by repeatedly appending to it. Since it is a linked list with no tail pointer, appending to it is O(n). The append method is found in regexp/syntax/compile.go, at line 42.

I don't know that this is exactly a bug, but it is surprising that although executing a regex is linear-time, compiling one isn't. It probably doesn't affect any useful regex, but it might be a DOS vector.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions