diff options
author | Robert Griesemer <gri@golang.org> | 2021-08-05 18:14:28 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-08-06 20:34:53 +0000 |
commit | 9bd1817e417e9f07c6b3aba0189576bbf06f1592 (patch) | |
tree | 4fb0ff8c493d29d757f3889e6ec85609ffee2be9 /src/cmd/compile/internal/types2/union.go | |
parent | 313924f2726947eb0df5f8fd0462c3a7343f5bc9 (diff) | |
download | go-9bd1817e417e9f07c6b3aba0189576bbf06f1592.tar.gz go-9bd1817e417e9f07c6b3aba0189576bbf06f1592.zip |
[dev.typeparams] cmd/compile/internal/types2: limit termlist lengths
At the moment, operations on termlists are O(n^2).
This is fine for normal-sized unions, but overlong
termlist lenghts will lead to excessive type checking
times.
Limit the length of termlists to avoid "compilations
that don't finish".
Change-Id: I39a7fc61b01c9db06faeb49a0e014b1ede532710
Reviewed-on: https://go-review.googlesource.com/c/go/+/340254
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/union.go')
-rw-r--r-- | src/cmd/compile/internal/types2/union.go | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/types2/union.go b/src/cmd/compile/internal/types2/union.go index 102826947c..85aa3d9104 100644 --- a/src/cmd/compile/internal/types2/union.go +++ b/src/cmd/compile/internal/types2/union.go @@ -43,12 +43,22 @@ func (t *Term) String() string { return (*term)(t).String() } // ---------------------------------------------------------------------------- // Implementation +// Avoid excessive type-checking times due to quadratic termlist operations. +const maxTermCount = 100 + +// parseUnion parses the given list of type expressions tlist as a union of +// those expressions. The result is a Union type, or Typ[Invalid] for some +// errors. func parseUnion(check *Checker, tlist []syntax.Expr) Type { var terms []*Term for _, x := range tlist { tilde, typ := parseTilde(check, x) if len(tlist) == 1 && !tilde { - return typ // single type + return typ // single type (optimization) + } + if len(terms) >= maxTermCount { + check.errorf(x, "cannot handle more than %d union terms (implementation limitation)", maxTermCount) + return Typ[Invalid] } terms = append(terms, NewTerm(tilde, typ)) } |