From: Bryan C. Mills Date: Tue, 9 Oct 2018 13:50:56 +0000 (-0400) Subject: cmd/go: check that package paths are invariantly non-empty X-Git-Tag: go1.12beta1~839 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6c85693bf14e246e5a53466048329cb7571a674c;p=gostls13.git cmd/go: check that package paths are invariantly non-empty The empty string is never a valid package path. Passing an empty string to a function that expects a package path indicates some missing validation step further up the call chain — typically (and most easily) a missed error check. Change-Id: I78a2403d95b473bacb0d40814cd9d477ecfd5351 Reviewed-on: https://go-review.googlesource.com/c/140857 Run-TryBot: Bryan C. Mills TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index ec2fa730c6..3d1b0e649d 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -440,6 +440,10 @@ const ( // this package, as part of a bigger load operation, and by GOPATH-based "go get". // TODO(rsc): When GOPATH-based "go get" is removed, unexport this function. func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package { + if path == "" { + panic("LoadImport called with empty package path") + } + stk.Push(path) defer stk.Pop() @@ -1750,6 +1754,9 @@ func LoadPackageNoFlags(arg string, stk *ImportStack) *Package { // loadPackage accepts pseudo-paths beginning with cmd/ to denote commands // in the Go command directory, as well as paths to those directories. func loadPackage(arg string, stk *ImportStack) *Package { + if arg == "" { + panic("loadPackage called with empty package path") + } if build.IsLocalImport(arg) { dir := arg if !filepath.IsAbs(dir) { @@ -1848,6 +1855,9 @@ func PackagesAndErrors(patterns []string) []*Package { for _, m := range matches { for _, pkg := range m.Pkgs { + if pkg == "" { + panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern)) + } p := loadPackage(pkg, &stk) p.Match = append(p.Match, m.Pattern) p.Internal.CmdlinePkg = true diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go index acee4a91e7..4e7741c5fb 100644 --- a/src/cmd/go/internal/modload/build.go +++ b/src/cmd/go/internal/modload/build.go @@ -30,6 +30,9 @@ func isStandardImportPath(path string) bool { } func findStandardImportPath(path string) string { + if path == "" { + panic("findStandardImportPath called with empty path") + } if search.IsStandardImportPath(path) { if goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) { return filepath.Join(cfg.GOROOT, "src", path) diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index 6c1525da9a..3b8c0b6435 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -397,6 +397,9 @@ func ModuleUsedDirectly(path string) bool { // Lookup requires that one of the Load functions in this package has already // been called. func Lookup(path string) (dir, realPath string, err error) { + if path == "" { + panic("Lookup called with empty package path") + } pkg, ok := loaded.pkgCache.Get(path).(*loadPkg) if !ok { // The loader should have found all the relevant paths.