Skip to content

cmd/cgo: never generate Go fields for zero-sized C fields #20275

@zagrodzki

Description

@zagrodzki

Please answer these questions before submitting your issue. Thanks!

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

tested with 1.7.5 and 1.8.1

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

linux/amd64

What did you do?

package main

/*
struct foo {
	int x;
	char padding;
	char y[];
};
*/
import "C"
import "fmt"

var Y C.struct_foo

func main() {
	fmt.Println(Y.y)
}

What did you expect to see?

Y.y undefined (type C.struct_foo has no field or method y)

(because Y.y is a zero-length field and Go code cannot refer to these fields)

What did you see instead?

Compiles and prints:

[]

I've checked that it's dependent on memory alignment. If the padding field is removed from struct foo, generated cgo type doesn't have the y field. With the padding, the field is included.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions