mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
cmd/compile: fix type checking of "make" arguments
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>
This commit is contained in:
parent
c4971a14a7
commit
ad0ab812f8
@ -1770,7 +1770,7 @@ func typecheck1(n *Node, top int) (res *Node) {
|
|||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
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
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
@ -1794,7 +1794,7 @@ func typecheck1(n *Node, top int) (res *Node) {
|
|||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
if !checkmake(t, "size", l) {
|
if !checkmake(t, "size", &l) {
|
||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
@ -1815,7 +1815,7 @@ func typecheck1(n *Node, top int) (res *Node) {
|
|||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
if !checkmake(t, "buffer", l) {
|
if !checkmake(t, "buffer", &l) {
|
||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
@ -3729,7 +3729,8 @@ ret:
|
|||||||
n.SetWalkdef(1)
|
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 {
|
if !n.Type.IsInteger() && n.Type.Etype != TIDEAL {
|
||||||
yyerror("non-integer %s argument in make(%v) - %v", arg, t, n.Type)
|
yyerror("non-integer %s argument in make(%v) - %v", arg, t, n.Type)
|
||||||
return false
|
return false
|
||||||
@ -3739,12 +3740,12 @@ func checkmake(t *types.Type, arg string, n *Node) bool {
|
|||||||
// to avoid redundant "constant NNN overflows int" errors.
|
// to avoid redundant "constant NNN overflows int" errors.
|
||||||
switch consttype(n) {
|
switch consttype(n) {
|
||||||
case CTINT, CTRUNE, CTFLT, CTCPLX:
|
case CTINT, CTRUNE, CTFLT, CTCPLX:
|
||||||
n.SetVal(toint(n.Val()))
|
v := toint(n.Val()).U.(*Mpint)
|
||||||
if n.Val().U.(*Mpint).CmpInt64(0) < 0 {
|
if v.CmpInt64(0) < 0 {
|
||||||
yyerror("negative %s argument in make(%v)", arg, t)
|
yyerror("negative %s argument in make(%v)", arg, t)
|
||||||
return false
|
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)
|
yyerror("%s argument too large in make(%v)", arg, t)
|
||||||
return false
|
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
|
// for instance, indexlit might be called here and incorporate some
|
||||||
// of the bounds checks done for make.
|
// of the bounds checks done for make.
|
||||||
n = defaultlit(n, types.Types[TINT])
|
n = defaultlit(n, types.Types[TINT])
|
||||||
|
*np = n
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
21
test/fixedbugs/issue41680.go
Normal file
21
test/fixedbugs/issue41680.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user