From d28242724872c6ab82d53a71fc775095d1171ee7 Mon Sep 17 00:00:00 2001 From: qeed Date: Mon, 20 Jun 2016 21:11:53 -0400 Subject: [PATCH] cmd/cgo: error, not panic, if not enough arguments to function Fixes #16116. Change-Id: Ic3cb0b95382bb683368743bda49b4eb5fdcc35c0 Reviewed-on: https://go-review.googlesource.com/24286 Reviewed-by: Emmanuel Odeke Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot --- misc/cgo/errors/issue16116.go | 12 ++++++++++++ misc/cgo/errors/test.bash | 1 + src/cmd/cgo/gcc.go | 12 +++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) create mode 100644 misc/cgo/errors/issue16116.go diff --git a/misc/cgo/errors/issue16116.go b/misc/cgo/errors/issue16116.go new file mode 100644 index 00000000000..1e01cab844e --- /dev/null +++ b/misc/cgo/errors/issue16116.go @@ -0,0 +1,12 @@ +// Copyright 2016 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 main + +// void f(void *p, int x) {} +import "C" + +func main() { + _ = C.f(1) // ERROR HERE +} diff --git a/misc/cgo/errors/test.bash b/misc/cgo/errors/test.bash index 429cec7627e..84d44d8a338 100755 --- a/misc/cgo/errors/test.bash +++ b/misc/cgo/errors/test.bash @@ -45,6 +45,7 @@ expect issue13129.go C.ushort check issue13423.go expect issue13635.go C.uchar C.schar C.ushort C.uint C.ulong C.longlong C.ulonglong C.complexfloat C.complexdouble check issue13830.go +check issue16116.go if ! go build issue14669.go; then exit 1 diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go index 3766ff27f09..fc1d01100d2 100644 --- a/src/cmd/cgo/gcc.go +++ b/src/cmd/cgo/gcc.go @@ -597,13 +597,15 @@ func (p *Package) rewriteCalls(f *File) { // rewriteCall rewrites one call to add pointer checks. We replace // each pointer argument x with _cgoCheckPointer(x).(T). func (p *Package) rewriteCall(f *File, call *Call, name *Name) { + // Avoid a crash if the number of arguments is + // less than the number of parameters. + // This will be caught when the generated file is compiled. + if len(call.Call.Args) < len(name.FuncType.Params) { + return + } + any := false for i, param := range name.FuncType.Params { - if len(call.Call.Args) <= i { - // Avoid a crash; this will be caught when the - // generated file is compiled. - return - } if p.needsPointerCheck(f, param.Go, call.Call.Args[i]) { any = true break