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
/*struct I { int i; int *p;};int f(struct I *x) { return x->i;}*/import"C"import (
"fmt"
)
typexstruct {
p*inti C.struct_I
}
funcmain() {
t1:=new(x)
t1.p=new(int)
t1.i.i=2fmt.Println("1:", C.f(&t1.i)) // <- this worksfmt.Println("2:", C.f((*C.struct_I)(&t1.i))) // <- this doesn't work
}
What did you expect to see?
1: 2
2: 2
What did you see instead?
1: 2
panic: runtime error: cgo argument has Go pointer to Go pointer
goroutine 1 [running]:
main.main.func2(0xc000090020, 0xc00009a008)
/home/notti/projects/cgocheck/main.go:30 +0x53
main.main()
/home/notti/projects/cgocheck/main.go:30 +0x114
Why does this happen?
checkAddr in src/cmd/cgo/gcc.go doesn't recognize type conversions containing parenthesis, which are required for pointers (see https://golang.org/ref/spec#Conversions), and, therefore emits a _cgoCheckPointer without true as second argument, causing the check to look at the whole struct instead of just the element. Looks like pointer type conversions should be supported since ast.StarExpr is handled in isType.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes. Tried also several older versions back to 1.10
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
What did you see instead?
Why does this happen?
checkAddr
insrc/cmd/cgo/gcc.go
doesn't recognize type conversions containing parenthesis, which are required for pointers (see https://golang.org/ref/spec#Conversions), and, therefore emits a_cgoCheckPointer
withouttrue
as second argument, causing the check to look at the wholestruct
instead of just the element. Looks like pointer type conversions should be supported sinceast.StarExpr
is handled inisType
.This is the cause for google/gopacket#664
Possible fix: https://go-review.googlesource.com/c/go/+/185098
(updated with better demonstration program)
The text was updated successfully, but these errors were encountered: