diff options
| author | Robert Griesemer <gri@google.com> | 2026-03-30 12:27:31 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-30 13:36:41 -0700 |
| commit | ed3ec75df47ab8e7d6e4a30c445a8ef771382584 (patch) | |
| tree | 3fcedc8fc366052dda1403c782fd10bfbcbb7dd7 /src | |
| parent | a4b534f5e42fe58d58c0ff0562d76680cedb0466 (diff) | |
| download | go-ed3ec75df47ab8e7d6e4a30c445a8ef771382584.tar.xz | |
go/types, types2: report an error if constant string overflows
Set a limit of 10 GiB for strings obtained via constant string addition.
Fixes #78346.
Change-Id: I35dbdff94f3ed32bf69654f4b3da435dad9f6236
Reviewed-on: https://go-review.googlesource.com/c/go/+/761300
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Jakub Ciolek <jakub@ciolek.dev>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/compile/internal/types2/const.go | 8 | ||||
| -rw-r--r-- | src/go/types/const.go | 8 | ||||
| -rw-r--r-- | src/internal/types/testdata/fixedbugs/issue78346.go | 34 |
3 files changed, 50 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types2/const.go b/src/cmd/compile/internal/types2/const.go index 74f45a6e8b..096187f515 100644 --- a/src/cmd/compile/internal/types2/const.go +++ b/src/cmd/compile/internal/types2/const.go @@ -46,6 +46,14 @@ func (check *Checker) overflow(x *operand, opPos syntax.Pos) { } check.errorf(atPos(opPos), InvalidConstVal, "constant %soverflow", op) x.val = constant.MakeUnknown() + return + } + + const maxLen = 10 * 1024 * 1024 + if x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen { + check.error(atPos(opPos), InvalidConstVal, "constant string too long") + x.val = constant.MakeUnknown() + return } } diff --git a/src/go/types/const.go b/src/go/types/const.go index f897df63a7..04bf47d439 100644 --- a/src/go/types/const.go +++ b/src/go/types/const.go @@ -48,6 +48,14 @@ func (check *Checker) overflow(x *operand, opPos token.Pos) { } check.errorf(atPos(opPos), InvalidConstVal, "constant %soverflow", op) x.val = constant.MakeUnknown() + return + } + + const maxLen = 10 * 1024 * 1024 + if x.val.Kind() == constant.String && len(constant.StringVal(x.val)) > maxLen { + check.error(atPos(opPos), InvalidConstVal, "constant string too long") + x.val = constant.MakeUnknown() + return } } diff --git a/src/internal/types/testdata/fixedbugs/issue78346.go b/src/internal/types/testdata/fixedbugs/issue78346.go new file mode 100644 index 0000000000..9db9891e13 --- /dev/null +++ b/src/internal/types/testdata/fixedbugs/issue78346.go @@ -0,0 +1,34 @@ +// Copyright 2026 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package p + +const ( + a = "x" + b = a + a + c = b + b + d = c + c + e = d + d + f = e + e + g = f + f + h = g + g + i = h + h + j = i + i + k = j + j + l = k + k + m = l + l + n = m + m + o = n + n + p = o + o + q = p + p + r = q + q + s = r + r + t = s + s + u = t + t + v = u + u + w = v + v + x = w + w + y = x + /* ERROR "constant string too long" */ x + z = y + y +) |
