]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modindex: apply changes in CL 502615 to modindex package
authorMichael Matloob <matloob@golang.org>
Tue, 1 Jul 2025 20:28:24 +0000 (16:28 -0400)
committerMichael Matloob <matloob@google.com>
Tue, 1 Jul 2025 21:05:14 +0000 (14:05 -0700)
CL 502615 modified go/build to check for invalid import paths, but did
not make those changes to the corresponding code in the modindex
package. Apply those changes here.

We should try to deduplicate the code to prevent this from happening
again.

For #73976
For #74446

Change-Id: I69fc5e2c829efb818c9974ec8126807a1c8f7913
Reviewed-on: https://go-review.googlesource.com/c/go/+/685317
TryBot-Bypass: Michael Matloob <matloob@google.com>
Auto-Submit: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Matloob <matloob@google.com>
src/cmd/go/internal/modindex/build_read.go

index 9137200123d17cfddd492749133c644026836469..f05b215c19e9ad242fa62c3de819b3e1e8534977 100644 (file)
@@ -15,6 +15,7 @@ import (
        "go/ast"
        "go/build"
        "go/parser"
+       "go/scanner"
        "go/token"
        "io"
        "strconv"
@@ -463,6 +464,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
                        }
@@ -520,6 +528,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;