From 581a822a9ed8fcae4afbc93daa6a74db7d9ea9a2 Mon Sep 17 00:00:00 2001 From: cuiweixie Date: Sat, 22 Oct 2022 22:07:07 +0800 Subject: [PATCH] regexp: add ErrLarge error For #56041 Change-Id: I6c98458b5c0d3b3636a53ee04fc97221f3fd8bbc Reviewed-on: https://go-review.googlesource.com/c/go/+/444817 TryBot-Result: Gopher Robot Reviewed-by: Bryan Mills Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Run-TryBot: xie cui <523516579@qq.com> --- api/next/56041.txt | 2 ++ src/regexp/all_test.go | 1 + src/regexp/syntax/parse.go | 9 +++++---- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 api/next/56041.txt diff --git a/api/next/56041.txt b/api/next/56041.txt new file mode 100644 index 00000000000..19cb164e7b8 --- /dev/null +++ b/api/next/56041.txt @@ -0,0 +1,2 @@ +pkg regexp/syntax, const ErrLarge = "expression too large" #56041 +pkg regexp/syntax, const ErrLarge ErrorCode #56041 diff --git a/src/regexp/all_test.go b/src/regexp/all_test.go index c233cfa9eaa..52de3fef83d 100644 --- a/src/regexp/all_test.go +++ b/src/regexp/all_test.go @@ -49,6 +49,7 @@ var badRe = []stringError{ {`a**`, "invalid nested repetition operator: `**`"}, {`a*+`, "invalid nested repetition operator: `*+`"}, {`\x`, "invalid escape sequence: `\\x`"}, + {strings.Repeat(`\pL`, 27000), "expression too large"}, } func compileTest(t *testing.T, expr string, error string) *Regexp { diff --git a/src/regexp/syntax/parse.go b/src/regexp/syntax/parse.go index 092dcfd5d05..accee9ab089 100644 --- a/src/regexp/syntax/parse.go +++ b/src/regexp/syntax/parse.go @@ -44,6 +44,7 @@ const ( ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression" ErrUnexpectedParen ErrorCode = "unexpected )" ErrNestingDepth ErrorCode = "expression nests too deeply" + ErrLarge ErrorCode = "expression too large" ) func (e ErrorCode) String() string { @@ -159,7 +160,7 @@ func (p *parser) reuse(re *Regexp) { func (p *parser) checkLimits(re *Regexp) { if p.numRunes > maxRunes { - panic(ErrInternalError) + panic(ErrLarge) } p.checkSize(re) p.checkHeight(re) @@ -203,7 +204,7 @@ func (p *parser) checkSize(re *Regexp) { } 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) case nil: // ok - case ErrInternalError: // too big - err = &Error{Code: ErrInternalError, Expr: s} + case ErrLarge: // too big + err = &Error{Code: ErrLarge, Expr: s} case ErrNestingDepth: err = &Error{Code: ErrNestingDepth, Expr: s} }