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/go: invalid -ldflags is silently ignored #43177

Open
hyangah opened this issue Dec 14, 2020 · 1 comment
Open

cmd/go: invalid -ldflags is silently ignored #43177

hyangah opened this issue Dec 14, 2020 · 1 comment
Milestone

Comments

@hyangah
Copy link
Contributor

@hyangah hyangah commented Dec 14, 2020

go version go1.15.5 linux/amd64

$ cat main.go
package main
var X = ""
func main() {	println(X)  }

$ go run -ldflags='-X main.X=Hello' main.go
Hello
$ go run -ldflags='"-X main.X=Hello"' main.go

$

The -ldflags of the second go run is in invalid format, but the go command silently ignores it and keeps working.

$ go run -ldflags='"-X main.X=Hello"' -x main.go
WORK=/tmp/go-build569857493
mkdir -p $WORK/b001/
...
mkdir -p $WORK/b001/exe/
cd .
$GOROOT/pkg/tool/linux_amd64/link -o $WORK/b001/exe/main -importcfg $WORK/b001/importcfg.link -s -w -buildmode=exe -buildid=HYVxxilVrLgdctP3U_YF/h7WWNLZkY13bQM5oqdBD/IUwm0pzLWLHdmcshsYuR/HYVxxilVrLgdctP3U_YF -extld=clang $WORK/b001/_pkg_.a
$WORK/b001/exe/main

See above link command. That's missing -X main.X=Hello

@bcmills
Copy link
Member

@bcmills bcmills commented Dec 14, 2020

From what I can tell, that flag is here:

BuildLdflags PerPackageFlag // -ldflags

That triggers the parsing case here:

if !strings.HasPrefix(v, "-") {
i := strings.Index(v, "=")
if i < 0 {
return fmt.Errorf("missing =<value> in <pattern>=<value>")
}
if i == 0 {
return fmt.Errorf("missing <pattern> in <pattern>=<value>")
}
pattern := strings.TrimSpace(v[:i])
match = MatchPackage(pattern, cwd)
v = v[i+1:]
}

...which succeeds because it finds =, and then attempts to match "-X main.X against a package path (which obviously fails to match anything because it's not a syntactically valid package path).

Before we call MatchPackage on the parsed pattern, we should first verify that it is a syntactically valid package pattern.

CC @jayconrod @matloob

@bcmills bcmills added this to the Backlog milestone Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.