Skip to content

inconsistent behavior with cgo and zero-length C arrays #13919

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

Closed
nsd20463 opened this issue Jan 12, 2016 · 3 comments
Closed

inconsistent behavior with cgo and zero-length C arrays #13919

nsd20463 opened this issue Jan 12, 2016 · 3 comments

Comments

@nsd20463
Copy link
Contributor

go version go1.5.2 linux/amd64

If there is a zero-length array at the end of a C struct which contains other fields, then cgo omits the field from the Go view of the C struct in $WORKDIR/.../_cgo_gotypes.go

But if the zero-length array is the only field in the struct then cgo keeps it. And if the zero-length array isn't last it keeps it.

In other words the code below fails to compile with the error
./tst27.go:19: t2.x undefined (type C.struct_T2 has no field or method x)

which is somewhat strange given that it is happy with the reference to the very similar t1.x.


package main

/*
struct T1 {
    char x[0];
};

struct T2 {
    int i;
    char x[0];
};
*/
import "C"

func main() {
    var t1 C.struct_T1
    _ = t1.x
    var t2 C.struct_T2
    _ = t2.x
}

(Also since Go is fine with zero-length arrays in Go structs I don't see why cgo is omitting T2.x)

@hirochachacha
Copy link
Contributor

If i am correct,

https://golang.org/doc/go1.5#cgo says:

When a C struct ends with a zero-sized field, but the struct itself is not zero-sized, Go code can no longer refer to the zero-sized field. Any such references will have to be rewritten.

You can also read #11925

@minux minux closed this as completed Jan 12, 2016
@minux
Copy link
Member

minux commented Jan 12, 2016 via email

@gopherbot
Copy link
Contributor

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

gopherbot pushed a commit that referenced this issue Jan 12, 2016
Update #9401.
Update #11925.
Update #13919.

Change-Id: I52c679353693e8165b2972d4d3974ee8bb1207ef
Reviewed-on: https://go-review.googlesource.com/18542
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Russ Cox <[email protected]>
@golang golang locked and limited conversation to collaborators Jan 13, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants