mirror of
https://github.com/golang/go.git
synced 2025-10-19 19:13:18 +00:00
cmd/go/internal/modindex: apply changes in CL 502615 to modindex package
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>
This commit is contained in:
parent
2f653a5a9e
commit
de646d94f7
1 changed files with 22 additions and 0 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue