diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index c9bd9be03c..c96acb74c9 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -3432,3 +3432,21 @@ func TestMatchesOnlySubtestParallelIsOK(t *testing.T) { tg.grepBothNot(noMatchesPattern, "go test did say [no tests to run]") tg.grepBoth(okPattern, "go test did not say ok") } + +func TestLinkXImportPathEscape(t *testing.T) { + // golang.org/issue/16710 + tg := testgo(t) + defer tg.cleanup() + tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata")) + exe := "./linkx" + exeSuffix + tg.creatingTemp(exe) + tg.run("build", "-o", exe, "-ldflags", "-X=my.pkg.Text=linkXworked", "my.pkg/main") + out, err := exec.Command(exe).CombinedOutput() + if err != nil { + tg.t.Fatal(err) + } + if string(out) != "linkXworked\n" { + tg.t.Log(string(out)) + tg.t.Fatal(`incorrect output: expected "linkXworked\n"`) + } +} diff --git a/src/cmd/go/testdata/src/my.pkg/main/main.go b/src/cmd/go/testdata/src/my.pkg/main/main.go new file mode 100644 index 0000000000..397e8b66a2 --- /dev/null +++ b/src/cmd/go/testdata/src/my.pkg/main/main.go @@ -0,0 +1,5 @@ +package main +import "my.pkg" +func main() { + println(pkg.Text) +} diff --git a/src/cmd/go/testdata/src/my.pkg/pkg.go b/src/cmd/go/testdata/src/my.pkg/pkg.go new file mode 100644 index 0000000000..0a5466ef17 --- /dev/null +++ b/src/cmd/go/testdata/src/my.pkg/pkg.go @@ -0,0 +1,2 @@ +package pkg +var Text = "unset" diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index 73c0daa77c..5197cb99b4 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -1025,11 +1025,12 @@ func strnputPad(s string, n int, pad []byte) { var strdata []*Symbol func addstrdata1(ctxt *Link, arg string) { - i := strings.Index(arg, "=") - if i < 0 { + eq := strings.Index(arg, "=") + dot := strings.LastIndex(arg[:eq+1], ".") + if eq < 0 || dot < 0 { Exitf("-X flag requires argument of the form importpath.name=value") } - addstrdata(ctxt, arg[:i], arg[i+1:]) + addstrdata(ctxt, pathtoprefix(arg[:dot])+arg[dot:eq], arg[eq+1:]) } func addstrdata(ctxt *Link, name string, value string) {