Skip to content

cgo: unexpected panic when passing a pointer to slice contents to cgo function #15318

Closed
@valyala

Description

@valyala

Please answer these questions before submitting your issue. Thanks!

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

go version devel +89a1f02 Fri Apr 15 10:17:17 2016 +0000 linux/amd64

2. What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GORACE=""
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build118843672=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

3. What did you do?

Run the following code:

package main

// static void foo(void *p) {}
import "C"

import (
        "encoding/json"
        "unsafe"
)

func main() {
        b, err := json.Marshal(123)
        if err != nil {
                panic(err)
        }
        // panic disappears if the following line is uncommented:
        // b = []byte("foobar")
        p := unsafe.Pointer(&b[0])
        C.foo(p)
}

4. What did you expect to see?

The program must complete without panics.

5. What did you see instead?

The following panic:

panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
panic(0x4a0360, 0xc82000c3b0)
    /home/aliaksandr/work/go1.5/src/runtime/panic.go:500 +0x18a
main._cgoCheckPointer0(0x497be0, 0xc820084024, 0x0, 0x0, 0x0, 0x0)
    command-line-arguments/_obj/_cgo_gotypes.go:38 +0x49
main.main()
    /home/aliaksandr/work/ybc/1.go:17 +0xa9

This panic is reproducible on go 1.6 too - see valyala/ybc#14 for details.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions