]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: error, not panic, if not enough arguments to function
authorqeed <qeed.quan@gmail.com>
Tue, 21 Jun 2016 01:11:53 +0000 (21:11 -0400)
committerIan Lance Taylor <iant@golang.org>
Tue, 21 Jun 2016 04:32:04 +0000 (04:32 +0000)
Fixes #16116.

Change-Id: Ic3cb0b95382bb683368743bda49b4eb5fdcc35c0
Reviewed-on: https://go-review.googlesource.com/24286
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

misc/cgo/errors/issue16116.go [new file with mode: 0644]
misc/cgo/errors/test.bash
src/cmd/cgo/gcc.go

diff --git a/misc/cgo/errors/issue16116.go b/misc/cgo/errors/issue16116.go
new file mode 100644 (file)
index 0000000..1e01cab
--- /dev/null
@@ -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
+}
index 429cec7627ef3a25a0426da5c322fa57209f6ba9..84d44d8a338443d65683412ec4a454d9bc58c948 100755 (executable)
@@ -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
index 3766ff27f0982ae7b613e84f91a01493650bf19b..fc1d01100d2ad1dd7ddddc7c341b8d80fdadb315 100644 (file)
@@ -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