diff options
author | Michael Matloob <matloob@golang.org> | 2023-06-12 12:33:30 -0400 |
---|---|---|
committer | Michael Pratt <mpratt@google.com> | 2023-06-13 19:34:05 +0000 |
commit | f5172dcd38f42829d145afd47c293afab934ccbc (patch) | |
tree | 3c4fc9563cfa93255449608ae988420adbb1a8e2 | |
parent | 8b3acefcbec3b861b8be9a37229a3d1329a9df1d (diff) | |
download | go-f5172dcd38f42829d145afd47c293afab934ccbc.tar.gz go-f5172dcd38f42829d145afd47c293afab934ccbc.zip |
[release-branch.go1.20] 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 #60754
Updates #60230
Updates #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>
(cherry picked from commit 962753b015407c69dd334578fd32a80aa7905c24)
Reviewed-on: https://go-review.googlesource.com/c/go/+/502697
-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 52adfeab9a..adcf82f3f0 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 } @@ -504,6 +512,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; |