]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: make env -w and -u validate GOOS and GOARCH values
authorjsign <jsign.uy@gmail.com>
Sat, 9 Nov 2019 14:41:09 +0000 (14:41 +0000)
committerBryan C. Mills <bcmills@google.com>
Sun, 10 Nov 2019 13:41:45 +0000 (13:41 +0000)
This change makes go env -w and -u check invalid GOOS and GOARCH values and abort if that's the case.

Fixes #34194

Change-Id: Idca8e93bb0b190fd273bf786c925be7993c24a2b
GitHub-Last-Rev: ee67f09d75f4552001cb8b6506bc4af0894c9b05
GitHub-Pull-Request: golang/go#34221
Reviewed-on: https://go-review.googlesource.com/c/go/+/194617
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/envcmd/env.go
src/cmd/go/internal/work/action.go
src/cmd/go/testdata/script/env_write.txt

index ff4a7e4a46aecceb80948c55db84cee4c91b2d8c..d2d5ed95076a2f202bc0a0a35b6c75ce89293fbf 100644 (file)
@@ -8,6 +8,7 @@ package envcmd
 import (
        "encoding/json"
        "fmt"
+       "go/build"
        "io/ioutil"
        "os"
        "path/filepath"
@@ -249,6 +250,21 @@ func runEnv(cmd *base.Command, args []string) {
                                fmt.Fprintf(os.Stderr, "warning: go env -w %s=... does not override conflicting OS environment variable\n", key)
                        }
                }
+
+               goos, okGOOS := add["GOOS"]
+               goarch, okGOARCH := add["GOARCH"]
+               if okGOOS || okGOARCH {
+                       if !okGOOS {
+                               goos = cfg.Goos
+                       }
+                       if !okGOARCH {
+                               goarch = cfg.Goarch
+                       }
+                       if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
+                               base.Fatalf("go env -w: %v", err)
+                       }
+               }
+
                updateEnvFile(add, nil)
                return
        }
@@ -265,6 +281,24 @@ func runEnv(cmd *base.Command, args []string) {
                        }
                        del[arg] = true
                }
+               if del["GOOS"] || del["GOARCH"] {
+                       goos, goarch := cfg.Goos, cfg.Goarch
+                       if del["GOOS"] {
+                               goos = getOrigEnv("GOOS")
+                               if goos == "" {
+                                       goos = build.Default.GOOS
+                               }
+                       }
+                       if del["GOARCH"] {
+                               goarch = getOrigEnv("GOARCH")
+                               if goarch == "" {
+                                       goarch = build.Default.GOARCH
+                               }
+                       }
+                       if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
+                               base.Fatalf("go env -u: %v", err)
+                       }
+               }
                updateEnvFile(nil, del)
                return
        }
@@ -331,6 +365,15 @@ func printEnvAsJSON(env []cfg.EnvVar) {
        }
 }
 
+func getOrigEnv(key string) string {
+       for _, v := range cfg.OrigEnv {
+               if strings.HasPrefix(v, key+"=") {
+                       return strings.TrimPrefix(v, key+"=")
+               }
+       }
+       return ""
+}
+
 func checkEnvWrite(key, val string) error {
        switch key {
        case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR":
index 0f35739976c40a3b57e9c60fde096ac2f4ee783f..391306a8d95e1b414626a36c9f5d69973c3d5e78 100644 (file)
@@ -290,11 +290,12 @@ func (b *Builder) Init() {
                }
        }
 
-       if _, ok := cfg.OSArchSupportsCgo[cfg.Goos+"/"+cfg.Goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
-               fmt.Fprintf(os.Stderr, "cmd/go: unsupported GOOS/GOARCH pair %s/%s\n", cfg.Goos, cfg.Goarch)
+       if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil {
+               fmt.Fprintf(os.Stderr, "cmd/go: %v", err)
                base.SetExitStatus(2)
                base.Exit()
        }
+
        for _, tag := range cfg.BuildContext.BuildTags {
                if strings.Contains(tag, ",") {
                        fmt.Fprintf(os.Stderr, "cmd/go: -tags space-separated list contains comma\n")
@@ -304,6 +305,13 @@ func (b *Builder) Init() {
        }
 }
 
+func CheckGOOSARCHPair(goos, goarch string) error {
+       if _, ok := cfg.OSArchSupportsCgo[goos+"/"+goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
+               return fmt.Errorf("unsupported GOOS/GOARCH pair %s/%s", goos, goarch)
+       }
+       return nil
+}
+
 // NewObjdir returns the name of a fresh object directory under b.WorkDir.
 // It is up to the caller to call b.Mkdir on the result at an appropriate time.
 // The result ends in a slash, so that file names in that directory
index 8b9c1bbf4509ff8d72db6fdd6f8a275fdfac7dc0..2366c3f580f4da7423af0f39555794caf96d9aa3 100644 (file)
@@ -96,3 +96,24 @@ stderr 'GOPATH entry cannot start with shell metacharacter'
 
 ! go env -w GOPATH=./go
 stderr 'GOPATH entry is relative; must be absolute path'
+
+# go env -w/-u checks validity of GOOS/ARCH combinations
+env GOOS=
+env GOARCH=
+# check -w doesn't allow invalid GOOS
+! go env -w GOOS=linuxx
+stderr 'unsupported GOOS/GOARCH pair linuxx'
+# check -w doesn't allow invalid GOARCH
+! go env -w GOARCH=amd644
+stderr 'unsupported GOOS/GOARCH.*/amd644$'
+# check -w doesn't allow invalid GOOS with valid GOARCH
+! go env -w GOOS=linuxx GOARCH=amd64
+stderr 'unsupported GOOS/GOARCH pair linuxx'
+# check a valid GOOS and GOARCH values but an incompatible combinations
+! go env -w GOOS=android GOARCH=s390x
+stderr 'unsupported GOOS/GOARCH pair android/s390x'
+# check that -u considers explicit envs
+go env -w GOOS=linux GOARCH=mips
+env GOOS=windows
+! go env -u GOOS
+stderr 'unsupported GOOS/GOARCH.*windows/mips$'