You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
package main
// static void foo(void *p) {}import"C"import (
"encoding/json""unsafe"
)
funcmain() {
b, err:=json.Marshal(123)
iferr!=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.
The text was updated successfully, but these errors were encountered:
@ianlancetaylor , are there workarounds for the following case?
package main
// struct bar { void *p; };// static void foo(struct bar b) {}import"C"import (
"encoding/json""unsafe"
)
funcmain() {
b, err:=json.Marshal(123)
iferr!=nil {
panic(err)
}
varbar C.struct_bar// panic disappears if the following line is uncommented:// b = []byte("foobar")bar.p=unsafe.Pointer(&b[0])
C.foo(bar)
}
What is messing up is that encoding/json uses a 64 byte buffer in a larger struct, and if the encoding requires 64 bytes or fewer than the resulting pointer is into that struct. So one workaround is
if len(b) <= 64 {
b = append([]byte(nil), b...)
}
bar.p = unsafe.Pointer(&b[0])
C.foo(bar)
Please answer these questions before submitting your issue. Thanks!
1. What version of Go are you using (
go version
)?2. What operating system and processor architecture are you using (
go env
)?3. What did you do?
Run the following code:
4. What did you expect to see?
The program must complete without panics.
5. What did you see instead?
The following panic:
This panic is reproducible on go 1.6 too - see valyala/ybc#14 for details.
The text was updated successfully, but these errors were encountered: