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: chokes on quoted defines #26025

Closed
karalabe opened this issue Jun 23, 2018 · 4 comments
Closed

cmd/cgo: chokes on quoted defines #26025

karalabe opened this issue Jun 23, 2018 · 4 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@karalabe
Copy link
Contributor

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

go version go1.10.1 linux/amd64

Does this issue reproduce with the latest release?

I guess, don't see why it wouldn't :P

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

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/karalabe/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/work"
GORACE=""
GOROOT="/opt/google/go"
GOTMPDIR=""
GOTOOLDIR="/opt/google/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build670060735=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Some C libraries (e.g. openssl) expect/require macros containing a quoted string. However, CGO chokes on quotes failing with malformed argument errors:

package main

/*
#cgo CFLAGS: -DMYDEFINE='"quotedstring"'
*/
import "C"

func main() {}

What did you expect to see?

I expect this file to build/run successfully.

What did you see instead?

$ go run main.go 
package main: [...]/main.go: malformed #cgo argument: -DMYDEFINE="quotedstring"

Note, I can hack around this issue by replacing the CGO preamble statement with a direct define

package main

/*
#define MYDEFINE "quotedstring"
*/
import "C"

func main() {}

but the issue is that I would need to inject this into all CGO enabled Go files, opposed to just having a single CGO preamble file for all the sources.

@tklauser tklauser changed the title CGO chokes on quoted defines cmd/cgo: chokes on quoted defines Jun 25, 2018
@tklauser
Copy link
Member

/cc @ianlancetaylor

@tklauser tklauser added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 25, 2018
@ianlancetaylor
Copy link
Contributor

We take a cautious approach to the characters permitted in #cgo directives, to avoid any possible security implications. The current restrictions are implemented in code at https://golang.org/src/go/build/build.go#L1368 .

The #cgo CFLAGS directive only applies to the current package, so it does not seem like an enormous burden to add the #define line to each cgo-using file in a single package.

I'm going to close this as working-as-expected. Please comment if you disagree.

@karalabe
Copy link
Contributor Author

@ianlancetaylor That seems a reasonable explanation and restriction. Perhaps one suggestion would be to add a note about it to https://golang.org/cmd/cgo/ so people have a means to find this limitation.

@gopherbot
Copy link

Change https://golang.org/cl/120975 mentions this issue: cmd/cgo: mention character restrictions on #cgo lines

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Jun 26, 2018
@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 26, 2018
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 26, 2018
@golang golang locked and limited conversation to collaborators Jun 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants