diff options
author | Rob Findley <rfindley@google.com> | 2021-02-10 12:04:31 -0500 |
---|---|---|
committer | Robert Findley <rfindley@google.com> | 2021-02-13 00:39:48 +0000 |
commit | 060fa49bd23d758a9062f4cb50e65960ec9662f1 (patch) | |
tree | 14a573ffc1dfab098ea4ceb275852334d3170ac4 /src/go/types/expr.go | |
parent | baa6c75dcef23aa51e95bf7818b7ded5262fbaa8 (diff) | |
download | go-060fa49bd23d758a9062f4cb50e65960ec9662f1.tar.gz go-060fa49bd23d758a9062f4cb50e65960ec9662f1.zip |
[dev.regabi] go/types: refuse excessively long constants
This is a port of CL 289049 to go/types. In that CL, tests were written
using the ability of tests/run.go to generate test packages dynamically.
For this CL, similar functionality is added to the go/types errmap
tests: tests are refactored to decouple the loading of source code from
the filesystem, so that tests for long constants may be generated
dynamically rather than checked-in as a large testdata file.
Change-Id: I92c7cb61a8d42c6593570ef7ae0af86b501fa34e
Reviewed-on: https://go-review.googlesource.com/c/go/+/290949
Trust: Robert Findley <rfindley@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/go/types/expr.go')
-rw-r--r-- | src/go/types/expr.go | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/go/types/expr.go b/src/go/types/expr.go index 5e1fe28a43..1a3c486af7 100644 --- a/src/go/types/expr.go +++ b/src/go/types/expr.go @@ -1140,6 +1140,23 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind { goto Error case *ast.BasicLit: + switch e.Kind { + case token.INT, token.FLOAT, token.IMAG: + // The max. mantissa precision for untyped numeric values + // is 512 bits, or 4048 bits for each of the two integer + // parts of a fraction for floating-point numbers that are + // represented accurately in the go/constant package. + // Constant literals that are longer than this many bits + // are not meaningful; and excessively long constants may + // consume a lot of space and time for a useless conversion. + // Cap constant length with a generous upper limit that also + // allows for separators between all digits. + const limit = 10000 + if len(e.Value) > limit { + check.errorf(e, _InvalidConstVal, "excessively long constant: %s... (%d chars)", e.Value[:10], len(e.Value)) + goto Error + } + } x.setConst(e.Kind, e.Value) if x.mode == invalid { // The parser already establishes syntactic correctness. |