]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[release-branch.go1.20] go/build: check for invalid import paths again
authorMichael Matloob <matloob@golang.org>
Mon, 12 Jun 2023 16:33:30 +0000 (12:33 -0400)
committerMichael Pratt <mpratt@google.com>
Tue, 13 Jun 2023 19:34:05 +0000 (19:34 +0000)
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

src/cmd/go/testdata/script/list_empty_import.txt [new file with mode: 0644]
src/go/build/read.go

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 (file)
index 0000000..4d76f09
--- /dev/null
@@ -0,0 +1,9 @@
+! go list a.go
+! stdout .
+stderr 'invalid import path'
+! stderr panic
+
+-- a.go --
+package a
+
+import ""
index 52adfeab9ae14c5196d5b994b4b0e3e10f0a5be9..adcf82f3f0e603415869a9b1c2919983fdd4d507 100644 (file)
@@ -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;