Skip to content

cmd/cgo: panic while checking **unsafe.Pointer in cgo call #13830

@xlab

Description

@xlab

What version of Go are you using (go version)?

$ go version
go version go1.6beta1 darwin/amd64

What operating system and processor architecture are you using?

$ uname -a
Darwin xlab-dev.local 15.2.0 Darwin Kernel Version 15.2.0: Fri Nov 13 19:56:56 PST 2015; root:xnu-3248.20.55~2/RELEASE_X86_64 x86_64

What did you do?

I'm trying to pass a pointer into a cgo call. The program below is a full demo.

package main

import "unsafe"

// typedef void Bar;
// void Foo(Bar** ptr) {};
import "C"

type Bar [0]byte

func Foo(arg **Bar) {
    ptr := (**C.Bar)(unsafe.Pointer(arg))
    C.Foo(ptr)
}

func main() {
    var ptr *Bar
    Foo(&ptr)
}

What did you expect to see?

The pointer successfully passed or a failure because of Go 1.6 restrictions against passing a pointer to Go allocated memory that has pointers to Go allocated memory.

What did you see instead?

$ go run main.go
panic: interface conversion: interface {} is **main._Ctype_Bar, not *unsafe.Pointer

goroutine 1 [running]:
main._cgoCheckPointer0(0x40552e0, 0xc82002a008, 0x0, 0x0, 0x0, 0x4056420)
    command-line-arguments/_obj/_cgo_gotypes.go:40 +0x91
main.Foo(0xc82002a008)
    cgofail/main.go:13 +0x48
main.main()
    cgofail/main.go:18 +0x31
exit status 2

The helper has been generated as:

func _cgoCheckPointer0(p interface{}, args ...interface{}) *unsafe.Pointer {
    return _cgoCheckPointer(p, args...).(*unsafe.Pointer)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions