]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: check that package paths are invariantly non-empty
authorBryan C. Mills <bcmills@google.com>
Tue, 9 Oct 2018 13:50:56 +0000 (09:50 -0400)
committerBryan C. Mills <bcmills@google.com>
Tue, 9 Oct 2018 20:10:02 +0000 (20:10 +0000)
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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/modload/build.go
src/cmd/go/internal/modload/load.go

index ec2fa730c69bc1ec24a33cbd8339418ee6436cd8..3d1b0e649d7f192624ca5855580d37c74ffaace8 100644 (file)
@@ -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
index acee4a91e7ce3e93d18ff9836b498525e937dc9e..4e7741c5fb7834ac858d0c2e2e38f48ec02a83cd 100644 (file)
@@ -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)
index 6c1525da9a4d252c00b049df99935382a1a8ad2e..3b8c0b6435f56ebf7189905dd753b2bac7740632 100644 (file)
@@ -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.