Skip to content

Commit

Permalink
regexp/syntax: add and use ErrInvalidDepth
Browse files Browse the repository at this point in the history
The fix for #51112 introduced a depth check but used
ErrInternalError to avoid introduce new API in a CL that
would be backported to earlier releases.

New API accepted in proposal #51684.

This CL adds a distinct error for this case.

For #51112.
Fixes #51684.

Change-Id: I068fc70aafe4218386a06103d9b7c847fb7ffa65
Reviewed-on: https://go-review.googlesource.com/c/go/+/384617
Trust: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
TryBot-Result: Gopher Robot <[email protected]>
  • Loading branch information
rsc committed Apr 4, 2022
1 parent 492c85a commit 1af60b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api/next/regexpdepth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg regexp/syntax, const ErrInvalidDepth = "invalid nesting depth" #0
pkg regexp/syntax, const ErrInvalidDepth ErrorCode #0

7 changes: 4 additions & 3 deletions src/regexp/syntax/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
ErrMissingRepeatArgument ErrorCode = "missing argument to repetition operator"
ErrTrailingBackslash ErrorCode = "trailing backslash at end of expression"
ErrUnexpectedParen ErrorCode = "unexpected )"
ErrInvalidDepth ErrorCode = "invalid nesting depth"
)

func (e ErrorCode) String() string {
Expand Down Expand Up @@ -133,7 +134,7 @@ func (p *parser) checkHeight(re *Regexp) {
}
}
if p.calcHeight(re, true) > maxHeight {
panic(ErrInternalError)
panic(ErrInvalidDepth)
}
}

Expand Down Expand Up @@ -756,8 +757,8 @@ func parse(s string, flags Flags) (_ *Regexp, err error) {
panic(r)
case nil:
// ok
case ErrInternalError:
err = &Error{Code: ErrInternalError, Expr: s}
case ErrInvalidDepth:
err = &Error{Code: ErrInvalidDepth, Expr: s}
}
}()

Expand Down

0 comments on commit 1af60b2

Please sign in to comment.