Commit 6c85693b authored by Bryan C. Mills's avatar Bryan C. Mills

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 <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 48dc42b6
...@@ -440,6 +440,10 @@ const ( ...@@ -440,6 +440,10 @@ const (
// this package, as part of a bigger load operation, and by GOPATH-based "go get". // 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. // 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 { 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) stk.Push(path)
defer stk.Pop() defer stk.Pop()
...@@ -1750,6 +1754,9 @@ func LoadPackageNoFlags(arg string, stk *ImportStack) *Package { ...@@ -1750,6 +1754,9 @@ func LoadPackageNoFlags(arg string, stk *ImportStack) *Package {
// loadPackage accepts pseudo-paths beginning with cmd/ to denote commands // loadPackage accepts pseudo-paths beginning with cmd/ to denote commands
// in the Go command directory, as well as paths to those directories. // in the Go command directory, as well as paths to those directories.
func loadPackage(arg string, stk *ImportStack) *Package { func loadPackage(arg string, stk *ImportStack) *Package {
if arg == "" {
panic("loadPackage called with empty package path")
}
if build.IsLocalImport(arg) { if build.IsLocalImport(arg) {
dir := arg dir := arg
if !filepath.IsAbs(dir) { if !filepath.IsAbs(dir) {
...@@ -1848,6 +1855,9 @@ func PackagesAndErrors(patterns []string) []*Package { ...@@ -1848,6 +1855,9 @@ func PackagesAndErrors(patterns []string) []*Package {
for _, m := range matches { for _, m := range matches {
for _, pkg := range m.Pkgs { for _, pkg := range m.Pkgs {
if pkg == "" {
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern))
}
p := loadPackage(pkg, &stk) p := loadPackage(pkg, &stk)
p.Match = append(p.Match, m.Pattern) p.Match = append(p.Match, m.Pattern)
p.Internal.CmdlinePkg = true p.Internal.CmdlinePkg = true
......
...@@ -30,6 +30,9 @@ func isStandardImportPath(path string) bool { ...@@ -30,6 +30,9 @@ func isStandardImportPath(path string) bool {
} }
func findStandardImportPath(path string) string { func findStandardImportPath(path string) string {
if path == "" {
panic("findStandardImportPath called with empty path")
}
if search.IsStandardImportPath(path) { if search.IsStandardImportPath(path) {
if goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) { if goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {
return filepath.Join(cfg.GOROOT, "src", path) return filepath.Join(cfg.GOROOT, "src", path)
......
...@@ -397,6 +397,9 @@ func ModuleUsedDirectly(path string) bool { ...@@ -397,6 +397,9 @@ func ModuleUsedDirectly(path string) bool {
// Lookup requires that one of the Load functions in this package has already // Lookup requires that one of the Load functions in this package has already
// been called. // been called.
func Lookup(path string) (dir, realPath string, err error) { 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) pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
if !ok { if !ok {
// The loader should have found all the relevant paths. // The loader should have found all the relevant paths.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment