1
0
mirror of https://github.com/golang/go synced 2024-11-26 08:17:59 -07:00

regexp: add ErrLarge error

For #56041

Change-Id: I6c98458b5c0d3b3636a53ee04fc97221f3fd8bbc
Reviewed-on: https://go-review.googlesource.com/c/go/+/444817
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: xie cui <523516579@qq.com>
This commit is contained in:
cuiweixie 2022-10-22 22:07:07 +08:00 committed by Gopher Robot
parent 03f6d81fc7
commit 581a822a9e
3 changed files with 8 additions and 4 deletions

2
api/next/56041.txt Normal file
View File

@ -0,0 +1,2 @@
pkg regexp/syntax, const ErrLarge = "expression too large" #56041
pkg regexp/syntax, const ErrLarge ErrorCode #56041

View File

@ -49,6 +49,7 @@ var badRe = []stringError{
{`a**`, "invalid nested repetition operator: `**`"}, {`a**`, "invalid nested repetition operator: `**`"},
{`a*+`, "invalid nested repetition operator: `*+`"}, {`a*+`, "invalid nested repetition operator: `*+`"},
{`\x`, "invalid escape sequence: `\\x`"}, {`\x`, "invalid escape sequence: `\\x`"},
{strings.Repeat(`\pL`, 27000), "expression too large"},
} }
func compileTest(t *testing.T, expr string, error string) *Regexp { func compileTest(t *testing.T, expr string, error string) *Regexp {

View File

@ -44,6 +44,7 @@ const (
ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression" ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression"
ErrUnexpectedParen ErrorCode = "unexpected )" ErrUnexpectedParen ErrorCode = "unexpected )"
ErrNestingDepth ErrorCode = "expression nests too deeply" ErrNestingDepth ErrorCode = "expression nests too deeply"
ErrLarge ErrorCode = "expression too large"
) )
func (e ErrorCode) String() string { func (e ErrorCode) String() string {
@ -159,7 +160,7 @@ func (p *parser) reuse(re *Regexp) {
func (p *parser) checkLimits(re *Regexp) { func (p *parser) checkLimits(re *Regexp) {
if p.numRunes > maxRunes { if p.numRunes > maxRunes {
panic(ErrInternalError) panic(ErrLarge)
} }
p.checkSize(re) p.checkSize(re)
p.checkHeight(re) p.checkHeight(re)
@ -203,7 +204,7 @@ func (p *parser) checkSize(re *Regexp) {
} }
if p.calcSize(re, true) > maxSize { if p.calcSize(re, true) > maxSize {
panic(ErrInternalError) panic(ErrLarge)
} }
} }
@ -897,8 +898,8 @@ func parse(s string, flags Flags) (_ *Regexp, err error) {
panic(r) panic(r)
case nil: case nil:
// ok // ok
case ErrInternalError: // too big case ErrLarge: // too big
err = &Error{Code: ErrInternalError, Expr: s} err = &Error{Code: ErrLarge, Expr: s}
case ErrNestingDepth: case ErrNestingDepth:
err = &Error{Code: ErrNestingDepth, Expr: s} err = &Error{Code: ErrNestingDepth, Expr: s}
} }