]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix type checking of "make" arguments
authorMatthew Dempsky <mdempsky@google.com>
Mon, 28 Sep 2020 19:19:56 +0000 (12:19 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 28 Sep 2020 20:54:13 +0000 (20:54 +0000)
As part of type checking make's arguments, we were converting untyped
float and complex constant arguments to integers. However, we were
doing this without concern for whether the argument was a declared
constant. Thus a call like "make([]T, n)" could change n from an
untyped float or untyped complex to an untyped integer.

The fix here is to simply change checkmake to not call SetVal, which
will be handled by defaultlit anyway. However, we also need to
properly return the defaultlit result value to the caller, so
checkmake's *Node parameter is also changed to **Node.

Fixes #41680.

Change-Id: I858927a052f384ec38684570d37b10a6906961f7
Reviewed-on: https://go-review.googlesource.com/c/go/+/257966
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue41680.go [new file with mode: 0644]

index 8e87fc9df0853d5ffbeb77ed5e787023ccfe2982..0eb0dae373c7d065882dad4c22659f4c8d7d5972 100644 (file)
@@ -1770,7 +1770,7 @@ func typecheck1(n *Node, top int) (res *Node) {
                                n.Type = nil
                                return n
                        }
-                       if !checkmake(t, "len", l) || r != nil && !checkmake(t, "cap", r) {
+                       if !checkmake(t, "len", &l) || r != nil && !checkmake(t, "cap", &r) {
                                n.Type = nil
                                return n
                        }
@@ -1794,7 +1794,7 @@ func typecheck1(n *Node, top int) (res *Node) {
                                        n.Type = nil
                                        return n
                                }
-                               if !checkmake(t, "size", l) {
+                               if !checkmake(t, "size", &l) {
                                        n.Type = nil
                                        return n
                                }
@@ -1815,7 +1815,7 @@ func typecheck1(n *Node, top int) (res *Node) {
                                        n.Type = nil
                                        return n
                                }
-                               if !checkmake(t, "buffer", l) {
+                               if !checkmake(t, "buffer", &l) {
                                        n.Type = nil
                                        return n
                                }
@@ -3729,7 +3729,8 @@ ret:
        n.SetWalkdef(1)
 }
 
-func checkmake(t *types.Type, arg string, n *Node) bool {
+func checkmake(t *types.Type, arg string, np **Node) bool {
+       n := *np
        if !n.Type.IsInteger() && n.Type.Etype != TIDEAL {
                yyerror("non-integer %s argument in make(%v) - %v", arg, t, n.Type)
                return false
@@ -3739,12 +3740,12 @@ func checkmake(t *types.Type, arg string, n *Node) bool {
        // to avoid redundant "constant NNN overflows int" errors.
        switch consttype(n) {
        case CTINT, CTRUNE, CTFLT, CTCPLX:
-               n.SetVal(toint(n.Val()))
-               if n.Val().U.(*Mpint).CmpInt64(0) < 0 {
+               v := toint(n.Val()).U.(*Mpint)
+               if v.CmpInt64(0) < 0 {
                        yyerror("negative %s argument in make(%v)", arg, t)
                        return false
                }
-               if n.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
+               if v.Cmp(maxintval[TINT]) > 0 {
                        yyerror("%s argument too large in make(%v)", arg, t)
                        return false
                }
@@ -3756,6 +3757,7 @@ func checkmake(t *types.Type, arg string, n *Node) bool {
        // for instance, indexlit might be called here and incorporate some
        // of the bounds checks done for make.
        n = defaultlit(n, types.Types[TINT])
+       *np = n
 
        return true
 }
diff --git a/test/fixedbugs/issue41680.go b/test/fixedbugs/issue41680.go
new file mode 100644 (file)
index 0000000..9dfeb7d
--- /dev/null
@@ -0,0 +1,21 @@
+// compile
+
+// 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 p
+
+func F(s string) bool {
+       const m = 16
+       const n = 1e5
+       _ = make([]int, n)
+       return len(s) < n*m
+}
+
+func G() {
+       const n = 1e5
+       _ = make([]int, n)
+       f := n
+       var _ float64 = f
+}