Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/cgo: panic instead of error for insufficient arguments to C function #13423

Closed
cookieo9 opened this issue Nov 28, 2015 · 3 comments

Comments

@cookieo9
Copy link
Contributor

commented Nov 28, 2015

There appears to be a regression in cgo, where when a C function is called with not enough arguments, it causes cgo to panic instead of printing a meaningful error:

package main

// #include <math.h>
// #cgo LDFLAGS: -lm
import "C"

func main() {
    _ = C.sin()
}

In go 1.4 go build prints out:
./foo.go:8: not enough arguments in call to _Cfunc_sin

At tip, the output is a panic during the execution of cgo:

panic: runtime error: index out of range

goroutine 1 [running]:
main.(*Package).rewriteCall(0xc82009e0f0, 0xc820082120, 0xc820076b00, 0xc8200a0180)
    /Users/cookieo9/local/go/src/cmd/cgo/gcc.go:606 +0xc58
main.(*Package).rewriteCalls(0xc82009e0f0, 0xc820082120)
    /Users/cookieo9/local/go/src/cmd/cgo/gcc.go:593 +0x1f8
main.(*Package).Translate(0xc82009e0f0, 0xc820082120)
    /Users/cookieo9/local/go/src/cmd/cgo/gcc.go:170 +0x197
main.main()
    /Users/cookieo9/local/go/src/cmd/cgo/main.go:275 +0x1055

Going the other way (i.e.: having too many arguments) produces the same error as go 1.4:
./foo.go:8: too many arguments in call to _Cfunc_sin

The only function I tested which doesn't panic, and prints the correct error is malloc, although I believe that's a result of special handling for malloc. Other functions I tested included "strcpy", "strlen", and "calloc" (from https://groups.google.com/forum/#!topic/golang-dev/TquSAX-w6NA).

I have tested this at tip on darwin-amd64 (El Capitain), and linux-arm (Raspberry Pi 2)

@rakyll

This comment has been minimized.

Copy link
Member

commented Nov 28, 2015

/cc @rsc

@ianlancetaylor ianlancetaylor added this to the Go1.6 milestone Nov 29, 2015

@ianlancetaylor ianlancetaylor self-assigned this Nov 29, 2015

@dominikh

This comment has been minimized.

Copy link
Member

commented Dec 1, 2015

This bug was introduced by 9dcc58c (cmd/cgo, runtime: add checks for passing pointers from Go to C). The code assumes that the call has at least as many arguments as the function definition requires.

/cc @ianlancetaylor

@gopherbot

This comment has been minimized.

Copy link

commented Dec 2, 2015

CL https://golang.org/cl/17332 mentions this issue.

@golang golang locked and limited conversation to collaborators Dec 1, 2016

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants
You can’t perform that action at this time.