diff options
author | Michael Matloob <matloob@golang.org> | 2023-06-12 12:33:30 -0400 |
---|---|---|
committer | Michael Matloob <matloob@golang.org> | 2023-06-12 19:15:55 +0000 |
commit | 962753b015407c69dd334578fd32a80aa7905c24 (patch) | |
tree | 532bccdf4b826a452c453f8c369c6a22066799b7 | |
parent | a674c6376feddfeef2e0e3c32bccd312b1a9b904 (diff) | |
download | go-962753b015407c69dd334578fd32a80aa7905c24.tar.gz go-962753b015407c69dd334578fd32a80aa7905c24.zip |
go/build: check for invalid import paths again
The go parser previously checked for invalid import paths, go/build,
seeing the parse error would put files with invalid import paths into
InvalidGoFiles. golang.org/cl/424855 removed that check from the
parser, which meant files with invalid import paths not have any parse
errors on them and not be put into InvalidGoFiles. Do a check for
invalid import paths in go/build soon after parsing so we can make
sure files with invalid import paths go into InvalidGoFiles.
This fixes an issue where the Go command assumed that if a file wasn't
invalid it had non empty import paths, leading to a panic.
Fixes #60230
Fixes #60686
Change-Id: I33c1dc9304649536834939cef7c689940236ee20
Reviewed-on: https://go-review.googlesource.com/c/go/+/502615
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
-rw-r--r-- | src/cmd/go/testdata/script/list_empty_import.txt | 9 | ||||
-rw-r--r-- | src/go/build/read.go | 22 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/cmd/go/testdata/script/list_empty_import.txt b/src/cmd/go/testdata/script/list_empty_import.txt new file mode 100644 index 0000000000..4d76f098b9 --- /dev/null +++ b/src/cmd/go/testdata/script/list_empty_import.txt @@ -0,0 +1,9 @@ +! go list a.go +! stdout . +stderr 'invalid import path' +! stderr panic + +-- a.go -- +package a + +import "" diff --git a/src/go/build/read.go b/src/go/build/read.go index 2f2e075a0c..52891975c1 100644 --- a/src/go/build/read.go +++ b/src/go/build/read.go @@ -11,6 +11,7 @@ import ( "fmt" "go/ast" "go/parser" + "go/scanner" "go/token" "io" "strconv" @@ -459,6 +460,13 @@ func readGoInfo(f io.Reader, info *fileInfo) error { if err != nil { return fmt.Errorf("parser returned invalid quoted string: <%s>", quoted) } + if !isValidImport(path) { + // The parser used to return a parse error for invalid import paths, but + // no longer does, so check for and create the error here instead. + info.parseErr = scanner.Error{Pos: info.fset.Position(spec.Pos()), Msg: "invalid import path: " + path} + info.imports = nil + return nil + } if path == "embed" { hasEmbed = true } @@ -516,6 +524,20 @@ func readGoInfo(f io.Reader, info *fileInfo) error { return nil } +// isValidImport checks if the import is a valid import using the more strict +// checks allowed by the implementation restriction in https://go.dev/ref/spec#Import_declarations. +// It was ported from the function of the same name that was removed from the +// parser in CL 424855, when the parser stopped doing these checks. +func isValidImport(s string) bool { + const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD" + for _, r := range s { + if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) { + return false + } + } + return s != "" +} + // parseGoEmbed parses the text following "//go:embed" to extract the glob patterns. // It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings. // This is based on a similar function in cmd/compile/internal/gc/noder.go; |