]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: detect -X setting non-string variable
authorRuss Cox <rsc@golang.org>
Mon, 29 Jun 2015 17:03:11 +0000 (13:03 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 29 Jun 2015 20:28:36 +0000 (20:28 +0000)
Fixes #9621.

Change-Id: Ib9c6001378364af899f57fd4b89fb23af2042923
Reviewed-on: https://go-review.googlesource.com/11694
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>

src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/pobj.go
test/linkx.go
test/linkx_run.go

index 52c6f4cd14b85b5f5ff8dc791cae2e4f4767c2bf..60b0be5ceb43142db316574eb693ee3e4d48f9d0 100644 (file)
@@ -917,6 +917,8 @@ func strnput(s string, n int) {
        }
 }
 
+var strdata []*LSym
+
 func addstrdata1(arg string) {
        i := strings.Index(arg, "=")
        if i < 0 {
@@ -944,9 +946,21 @@ func addstrdata(name string, value string) {
        // we know before entering this function.
        s.Reachable = reachable
 
+       strdata = append(strdata, s)
+
        sp.Reachable = reachable
 }
 
+func checkstrdata() {
+       for _, s := range strdata {
+               if s.Type == obj.STEXT {
+                       Diag("cannot use -X with text symbol %s", s.Name)
+               } else if s.Gotype != nil && s.Gotype.Name != "type.string" {
+                       Diag("cannot use -X with non-string symbol %s", s.Name)
+               }
+       }
+}
+
 func Addstring(s *LSym, str string) int64 {
        if s.Type == 0 {
                s.Type = obj.SNOPTRDATA
index fd541fbb3b5a4d1d359cebe64fbdf71c6c7df270..5ce197725bf5b30b56b6a6e8fd8fc21e88ee6b0a 100644 (file)
@@ -220,6 +220,7 @@ func Ldmain() {
        }
 
        checkgo()
+       checkstrdata()
        deadcode()
        callgraph()
 
index 40ec1b99f69219112d777e0c6ca09ee7dc150988..ac2033400f813856798091ca223c91ddf52bfd38 100644 (file)
@@ -14,6 +14,9 @@ import "fmt"
 var tbd string
 var overwrite string = "dibs"
 
+var b bool
+var x int
+
 func main() {
        fmt.Println(tbd)
        fmt.Println(overwrite)
index cc0f55cf9281f309e362472c2332b5ce3c5aaf8b..a6c7c67014cd4132e3798f316fbc65f24f73e799 100644 (file)
@@ -14,6 +14,7 @@ import (
        "fmt"
        "os"
        "os/exec"
+       "strings"
 )
 
 func main() {
@@ -49,4 +50,21 @@ func test(sep string) {
                fmt.Println("-X linker flag should not accept keys without values")
                os.Exit(1)
        }
+
+       // Issue 9621
+       cmd = exec.Command("go", "run", "-ldflags=-X main.b=false -X main.x=42", "linkx.go")
+       outx, err := cmd.CombinedOutput()
+       if err == nil {
+               fmt.Println("-X linker flag should not overwrite non-strings")
+               os.Exit(1)
+       }
+       outstr := string(outx)
+       if !strings.Contains(outstr, "main.b") {
+               fmt.Printf("-X linker flag did not diagnose overwrite of main.b\n")
+               os.Exit(1)
+       }
+       if !strings.Contains(outstr, "main.x") {
+               fmt.Printf("-X linker flag did not diagnose overwrite of main.x\n")
+               os.Exit(1)
+       }
 }