Skip to content

Commit 4d4ddd8

Browse files
jsignBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go: make env -w and -u validate GOOS and GOARCH values
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: ee67f09 GitHub-Pull-Request: #34221 Reviewed-on: https://go-review.googlesource.com/c/go/+/194617 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 9eb9c7b commit 4d4ddd8

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

src/cmd/go/internal/envcmd/env.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package envcmd
88
import (
99
"encoding/json"
1010
"fmt"
11+
"go/build"
1112
"io/ioutil"
1213
"os"
1314
"path/filepath"
@@ -249,6 +250,21 @@ func runEnv(cmd *base.Command, args []string) {
249250
fmt.Fprintf(os.Stderr, "warning: go env -w %s=... does not override conflicting OS environment variable\n", key)
250251
}
251252
}
253+
254+
goos, okGOOS := add["GOOS"]
255+
goarch, okGOARCH := add["GOARCH"]
256+
if okGOOS || okGOARCH {
257+
if !okGOOS {
258+
goos = cfg.Goos
259+
}
260+
if !okGOARCH {
261+
goarch = cfg.Goarch
262+
}
263+
if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
264+
base.Fatalf("go env -w: %v", err)
265+
}
266+
}
267+
252268
updateEnvFile(add, nil)
253269
return
254270
}
@@ -265,6 +281,24 @@ func runEnv(cmd *base.Command, args []string) {
265281
}
266282
del[arg] = true
267283
}
284+
if del["GOOS"] || del["GOARCH"] {
285+
goos, goarch := cfg.Goos, cfg.Goarch
286+
if del["GOOS"] {
287+
goos = getOrigEnv("GOOS")
288+
if goos == "" {
289+
goos = build.Default.GOOS
290+
}
291+
}
292+
if del["GOARCH"] {
293+
goarch = getOrigEnv("GOARCH")
294+
if goarch == "" {
295+
goarch = build.Default.GOARCH
296+
}
297+
}
298+
if err := work.CheckGOOSARCHPair(goos, goarch); err != nil {
299+
base.Fatalf("go env -u: %v", err)
300+
}
301+
}
268302
updateEnvFile(nil, del)
269303
return
270304
}
@@ -331,6 +365,15 @@ func printEnvAsJSON(env []cfg.EnvVar) {
331365
}
332366
}
333367

368+
func getOrigEnv(key string) string {
369+
for _, v := range cfg.OrigEnv {
370+
if strings.HasPrefix(v, key+"=") {
371+
return strings.TrimPrefix(v, key+"=")
372+
}
373+
}
374+
return ""
375+
}
376+
334377
func checkEnvWrite(key, val string) error {
335378
switch key {
336379
case "GOEXE", "GOGCCFLAGS", "GOHOSTARCH", "GOHOSTOS", "GOMOD", "GOTOOLDIR":

src/cmd/go/internal/work/action.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ func (b *Builder) Init() {
290290
}
291291
}
292292

293-
if _, ok := cfg.OSArchSupportsCgo[cfg.Goos+"/"+cfg.Goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
294-
fmt.Fprintf(os.Stderr, "cmd/go: unsupported GOOS/GOARCH pair %s/%s\n", cfg.Goos, cfg.Goarch)
293+
if err := CheckGOOSARCHPair(cfg.Goos, cfg.Goarch); err != nil {
294+
fmt.Fprintf(os.Stderr, "cmd/go: %v", err)
295295
base.SetExitStatus(2)
296296
base.Exit()
297297
}
298+
298299
for _, tag := range cfg.BuildContext.BuildTags {
299300
if strings.Contains(tag, ",") {
300301
fmt.Fprintf(os.Stderr, "cmd/go: -tags space-separated list contains comma\n")
@@ -304,6 +305,13 @@ func (b *Builder) Init() {
304305
}
305306
}
306307

308+
func CheckGOOSARCHPair(goos, goarch string) error {
309+
if _, ok := cfg.OSArchSupportsCgo[goos+"/"+goarch]; !ok && cfg.BuildContext.Compiler == "gc" {
310+
return fmt.Errorf("unsupported GOOS/GOARCH pair %s/%s", goos, goarch)
311+
}
312+
return nil
313+
}
314+
307315
// NewObjdir returns the name of a fresh object directory under b.WorkDir.
308316
// It is up to the caller to call b.Mkdir on the result at an appropriate time.
309317
// The result ends in a slash, so that file names in that directory

src/cmd/go/testdata/script/env_write.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,24 @@ stderr 'GOPATH entry cannot start with shell metacharacter'
9696

9797
! go env -w GOPATH=./go
9898
stderr 'GOPATH entry is relative; must be absolute path'
99+
100+
# go env -w/-u checks validity of GOOS/ARCH combinations
101+
env GOOS=
102+
env GOARCH=
103+
# check -w doesn't allow invalid GOOS
104+
! go env -w GOOS=linuxx
105+
stderr 'unsupported GOOS/GOARCH pair linuxx'
106+
# check -w doesn't allow invalid GOARCH
107+
! go env -w GOARCH=amd644
108+
stderr 'unsupported GOOS/GOARCH.*/amd644$'
109+
# check -w doesn't allow invalid GOOS with valid GOARCH
110+
! go env -w GOOS=linuxx GOARCH=amd64
111+
stderr 'unsupported GOOS/GOARCH pair linuxx'
112+
# check a valid GOOS and GOARCH values but an incompatible combinations
113+
! go env -w GOOS=android GOARCH=s390x
114+
stderr 'unsupported GOOS/GOARCH pair android/s390x'
115+
# check that -u considers explicit envs
116+
go env -w GOOS=linux GOARCH=mips
117+
env GOOS=windows
118+
! go env -u GOOS
119+
stderr 'unsupported GOOS/GOARCH.*windows/mips$'

0 commit comments

Comments
 (0)