diff options
Diffstat (limited to 'src/cmd/compile/internal/typecheck/typecheck.go')
-rw-r--r-- | src/cmd/compile/internal/typecheck/typecheck.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index 00dd44b96b..1650144375 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -775,6 +775,14 @@ func typecheck1(n ir.Node, top int) ir.Node { n := n.(*ir.CallExpr) return tcRecover(n) + case ir.OUNSAFEADD: + n := n.(*ir.BinaryExpr) + return tcUnsafeAdd(n) + + case ir.OUNSAFESLICE: + n := n.(*ir.BinaryExpr) + return tcUnsafeSlice(n) + case ir.OCLOSURE: n := n.(*ir.ClosureExpr) tcClosure(n, top) @@ -1934,6 +1942,35 @@ func checkmake(t *types.Type, arg string, np *ir.Node) bool { return true } +// checkunsafeslice is like checkmake but for unsafe.Slice. +func checkunsafeslice(np *ir.Node) bool { + n := *np + if !n.Type().IsInteger() && n.Type().Kind() != types.TIDEAL { + base.Errorf("non-integer len argument in unsafe.Slice - %v", n.Type()) + return false + } + + // Do range checks for constants before DefaultLit + // to avoid redundant "constant NNN overflows int" errors. + if n.Op() == ir.OLITERAL { + v := toint(n.Val()) + if constant.Sign(v) < 0 { + base.Errorf("negative len argument in unsafe.Slice") + return false + } + if ir.ConstOverflow(v, types.Types[types.TINT]) { + base.Errorf("len argument too large in unsafe.Slice") + return false + } + } + + // DefaultLit is necessary for non-constants too: n might be 1.1<<k. + n = DefaultLit(n, types.Types[types.TINT]) + *np = n + + return true +} + // markBreak marks control statements containing break statements with SetHasBreak(true). func markBreak(fn *ir.Func) { var labels map[*types.Sym]ir.Node |