# UNDF: UNDF-2026-000000825 diff --git a/src/cmd/compile/internal/types2/infer.go b/src/cmd/compile/internal/types2/infer.go index eeefb117..cwe407fix 100644 --- a/src/cmd/compile/internal/types2/infer.go +++ b/src/cmd/compile/internal/types2/infer.go @@ -543,14 +543,20 @@ func isParameterized(tparams []*TypeParam, typ Type) bool { - w := tpWalker{ - tparams: tparams, - seen: make(map[Type]bool), - } + // CWE-407 fix: pre-build a map for O(1) TypeParam membership check. + // Previously tpWalker used a []*TypeParam slice and called slices.Index + // (O(n)) at every *TypeParam node during the walk — O(n²) total for n + // type parameters in a generic function. + tset := make(map[*TypeParam]bool, len(tparams)) + for _, tp := range tparams { + tset[tp] = true + } + w := tpWalker{ + tparams: tset, + seen: make(map[Type]bool), + } return w.isParameterized(typ) } type tpWalker struct { - tparams []*TypeParam + tparams map[*TypeParam]bool // CWE-407: was []*TypeParam (O(n) contains) seen map[Type]bool } @@ -627,7 +633,7 @@ func (w *tpWalker) isParameterized(typ Type) (res bool) { case *TypeParam: - return slices.Index(w.tparams, t) >= 0 + return w.tparams[t] // CWE-407: O(1) map lookup, was O(n) slices.Index default: panic(fmt.Sprintf("unexpected %T", typ))