]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: report an error for invalid constant values
authorRobert Griesemer <gri@golang.org>
Thu, 19 Nov 2020 01:05:02 +0000 (17:05 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 19 Nov 2020 01:27:31 +0000 (01:27 +0000)
The parser reports syntactic errors in constant literals.
The go/constant package produces an "unknown" value for
syntactically correct numeric constants that are too small
or too large. Check for the unknown value and report an
error rather than silently continuing.

Fixes #42695.

Change-Id: I414214559a285d67ed50184dc750f106960b5620
Reviewed-on: https://go-review.googlesource.com/c/go/+/271377
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/go/types/expr.go
src/go/types/fixedbugs/issue42695.src [new file with mode: 0644]
src/go/types/operand.go

index 11f94112846602d5652edb4f9c3738562a7aaa7e..b026e99ce28356dda880ff54ccdf0614451dc896 100644 (file)
@@ -1073,7 +1073,11 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
        case *ast.BasicLit:
                x.setConst(e.Kind, e.Value)
                if x.mode == invalid {
-                       check.invalidAST(e, "invalid literal %v", e.Value)
+                       // The parser already establishes syntactic correctness.
+                       // If we reach here it's because of number under-/overflow.
+                       // TODO(gri) setConst (and in turn the go/constant package)
+                       // should return an error describing the issue.
+                       check.errorf(e, _InvalidConstVal, "malformed constant: %s", e.Value)
                        goto Error
                }
 
diff --git a/src/go/types/fixedbugs/issue42695.src b/src/go/types/fixedbugs/issue42695.src
new file mode 100644 (file)
index 0000000..d0d6200
--- /dev/null
@@ -0,0 +1,17 @@
+// Copyright 2020 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 issue42695
+
+const _ = 6e5518446744 // ERROR malformed constant
+const _ uint8 = 6e5518446744 // ERROR malformed constant
+
+var _ = 6e5518446744 // ERROR malformed constant
+var _ uint8 = 6e5518446744 // ERROR malformed constant
+
+func f(x int) int {
+        return x + 6e5518446744 // ERROR malformed constant
+}
+
+var _ = f(6e5518446744 /* ERROR malformed constant */ )
index 2d30dbd024c192c801be1e29f5df33b26a0cd77d..3e1ac312d9a5e46d33c1518c3f5cefbd9eb0198e 100644 (file)
@@ -195,9 +195,15 @@ func (x *operand) setConst(tok token.Token, lit string) {
                unreachable()
        }
 
+       val := constant.MakeFromLiteral(lit, tok, 0)
+       if val.Kind() == constant.Unknown {
+               x.mode = invalid
+               x.typ = Typ[Invalid]
+               return
+       }
        x.mode = constant_
        x.typ = Typ[kind]
-       x.val = constant.MakeFromLiteral(lit, tok, 0)
+       x.val = val
 }
 
 // isNil reports whether x is the nil value.