Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/regexp/syntax/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/regexp/syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,17 @@ func (p *parser) repeat(op Op, min, max int, before, after, lastRepeat string) (
p.stack[n-1] = re
p.checkLimits(re)

if op == OpRepeat && (min >= 2 || max >= 2) && !repeatIsValid(re, 1000) {
if op == OpRepeat && (min >= 2 || max >= 2) && !repeatIsValid(re, maxRepeat) {
return "", &Error{ErrInvalidRepeatSize, before[:len(before)-len(after)]}
}

return after, nil
}

// maxRepeat is the maximum count allowed in any single repetition x{n,m}.
// The product of all nested repetition counts must also not exceed this limit.
const maxRepeat = 1000

// repeatIsValid reports whether the repetition re is valid.
// Valid means that the combination of the top-level repetition
// and any inner repetitions does not exceed n copies of the
Expand Down Expand Up @@ -1000,7 +1004,7 @@ func parse(s string, flags Flags) (_ *Regexp, err error) {
t = t[1:]
break
}
if min < 0 || min > 1000 || max > 1000 || max >= 0 && min > max {
if min < 0 || min > maxRepeat || max > maxRepeat || max >= 0 && min > max {
// Numbers were too big, or max is present and min > max.
return nil, &Error{ErrInvalidRepeatSize, before[:len(before)-len(after)]}
}
Expand Down
2 changes: 2 additions & 0 deletions src/regexp/syntax/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ var parseTests = []parseTest{
// Valid repetitions.
{`((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}))`, ``},
{`((((((((((x{1}){2}){2}){2}){2}){2}){2}){2}){2}){2})`, ``},
{`(?:[a-z]{4}){0,250}`, ``}, // nested repeat product 4*250 = 1000, at the limit

// Valid nesting.
{strings.Repeat("(", 999) + strings.Repeat(")", 999), ``},
Expand Down Expand Up @@ -500,6 +501,7 @@ var invalidRegexps = []string{
`a{100000}`, // too much repetition
`a{100000,}`, // too much repetition
"((((((((((x{2}){2}){2}){2}){2}){2}){2}){2}){2}){2})", // too much repetition
`(?:[a-z]{4}){0,251}`, // nested repeat product 4*251 > 1000
strings.Repeat("(", 1000) + strings.Repeat(")", 1000), // too deep
strings.Repeat("(?:", 1000) + strings.Repeat(")*", 1000), // too deep
"(" + strings.Repeat("(xx?)", 1000) + "){1000}", // too long
Expand Down