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

cmd/cgo: C code with function taking pointer typedef leads to C compiler warning #19832

Open
ianlancetaylor opened this Issue Apr 4, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@ianlancetaylor
Contributor

ianlancetaylor commented Apr 4, 2017

Building this file gets a warning from the C compiler:

package main

// typedef struct { int i; } *PS;
// void F(PS p) {}
import "C"

func main() {
	C.F(nil)
}

The warning I see is:

# command-line-arguments
cgo-gcc-prolog: In function ‘_cgo_0164fa09626e_Cfunc_F’:
cgo-gcc-prolog:37:2: warning: passing argument 1 of ‘F’ from incompatible pointer type [enabled by default]
/home/iant/foo8.go:4:7: note: expected ‘PS’ but argument is of type ‘struct <anonymous> *’
 // void F(PS p) {}
       ^

The code generated by cgo looks like:

CGO_NO_SANITIZE_THREAD
void
_cgo_0164fa09626e_Cfunc_F(void *v)
{
	struct {
		struct {int i; }* p0;
	} __attribute__((__packed__, __gcc_struct__)) *a = v;
	_cgo_tsan_acquire();
	F(a->p0);
	_cgo_tsan_release();
}

The C function expects PS* but we are passing struct{int i;}*. That is, we aren't using the typedef.

Changing the Go code to use C.PS(unsafe.Pointer(nil)) also fails in the same way.

@gopherbot

This comment has been minimized.

gopherbot commented Jul 18, 2017

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

@gopherbot

This comment has been minimized.

gopherbot commented Feb 6, 2018

Change https://golang.org/cl/92455 mentions this issue: cmd/cgo: revert CL 49490 "fix for function taking pointer typedef"

gopherbot pushed a commit that referenced this issue Feb 7, 2018

cmd/cgo: revert CL 49490 "fix for function taking pointer typedef"
CL 49490 fixed a warning when compiling the C code generated by cgo,
but it introduced typedef conflicts in Go code that cgo is supposed to
avoid.

Original CL description:

    cmd/cgo: fix for function taking pointer typedef

    Fixes #19832

Updates #19832
Fixes #23720

Change-Id: I22a732db31be0b4f7248c105277ab8ee44ef6cfb
Reviewed-on: https://go-review.googlesource.com/92455
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Feb 7, 2018

Reopening to fix in 1.11, without breaking #23720 .

@ianlancetaylor ianlancetaylor reopened this Feb 7, 2018

@ianlancetaylor ianlancetaylor modified the milestones: Go1.10, Go1.11 Feb 7, 2018

@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018

Gskartwii added a commit to Gskartwii/windivert-go that referenced this issue Nov 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment