Skip to content
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
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

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.

@ianlancetaylor ianlancetaylor added this to the Go1.9 milestone Apr 4, 2017
@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 Jun 7, 2017
@bradfitz bradfitz added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Jun 7, 2017
@gopherbot
Copy link

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

@gopherbot
Copy link

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
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
Copy link
Contributor Author

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 pushed a commit to Gskartwii/windivert-go that referenced this issue Nov 10, 2018
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants