Skip to content

Commit 5a049aa

Browse files
cmd/cgo: error, not panic, if not enough arguments to function
Fixes golang#13423. Change-Id: I41bb45790cca36c57a107796f0eca61287acb2a9 Reviewed-on: https://go-review.googlesource.com/17332 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 8a34cf7 commit 5a049aa

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

misc/cgo/errors/issue13423.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright 2015 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
// #include <stdio.h>
8+
import "C"
9+
10+
func main() {
11+
_ = C.fopen() // ERROR HERE
12+
}

misc/cgo/errors/test.bash

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ check issue8442.go
4040
check issue11097a.go
4141
check issue11097b.go
4242
expect issue13129.go C.ushort
43+
check issue13423.go
4344

4445
if ! go run ptr.go; then
4546
exit 1

src/cmd/cgo/gcc.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,12 @@ func (p *Package) rewriteCalls(f *File) {
598598
// each pointer argument x with _cgoCheckPointer(x).(T).
599599
func (p *Package) rewriteCall(f *File, call *ast.CallExpr, name *Name) {
600600
for i, param := range name.FuncType.Params {
601+
if len(call.Args) <= i {
602+
// Avoid a crash; this will be caught when the
603+
// generated file is compiled.
604+
return
605+
}
606+
601607
// An untyped nil does not need a pointer check, and
602608
// when _cgoCheckPointer returns the untyped nil the
603609
// type assertion we are going to insert will fail.
@@ -611,12 +617,6 @@ func (p *Package) rewriteCall(f *File, call *ast.CallExpr, name *Name) {
611617
continue
612618
}
613619

614-
if len(call.Args) <= i {
615-
// Avoid a crash; this will be caught when the
616-
// generated file is compiled.
617-
return
618-
}
619-
620620
c := &ast.CallExpr{
621621
Fun: ast.NewIdent("_cgoCheckPointer"),
622622
Args: []ast.Expr{

0 commit comments

Comments
 (0)