1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:14:42 -07:00

go.tools/go/types: check validity of import paths

This check is currently done in go/parser as well but
eventually can be removed from there (after Go 1.2).

R=adonovan
CC=golang-dev
https://golang.org/cl/22240045
This commit is contained in:
Robert Griesemer 2013-11-08 14:14:58 -08:00
parent 7b183d1766
commit c798b9cca0
2 changed files with 33 additions and 1 deletions

View File

@ -10,6 +10,8 @@ import (
"go/ast"
"go/token"
"strconv"
"strings"
"unicode"
"code.google.com/p/go.tools/go/exact"
)
@ -93,6 +95,23 @@ func (check *checker) arityMatch(s, init *ast.ValueSpec) {
}
}
func (check *checker) validatedImportPath(path string) (string, error) {
s, err := strconv.Unquote(path)
if err != nil {
return "", err
}
if s == "" {
return "", fmt.Errorf("empty string")
}
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
for _, r := range s {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return s, fmt.Errorf("invalid character %#U", r)
}
}
return s, nil
}
// TODO(gri) Split resolveFiles into smaller components.
func (check *checker) resolveFiles(files []*ast.File) {
@ -158,7 +177,11 @@ func (check *checker) resolveFiles(files []*ast.File) {
case *ast.ImportSpec:
// import package
var imp *Package
path, _ := strconv.Unquote(s.Path.Value)
path, err := check.validatedImportPath(s.Path.Value)
if err != nil {
check.errorf(s.Path.Pos(), "invalid import path (%s)", err)
continue
}
if path == "C" && check.conf.FakeImportC {
// TODO(gri) shouldn't create a new one each time
imp = NewPackage("C", "C", NewScope(nil))

View File

@ -11,6 +11,15 @@ import . "testing" // declares T in file scope
import . /* ERROR "imported but not used" */ "unsafe"
import . "fmt" // declares Println in file scope
import (
// TODO(gri) At the moment, 2 errors are reported because both go/parser
// and the type checker report it. Eventually, this test should not be
// done by the parser anymore.
"" /* ERROR invalid import path */ /* ERROR invalid import path */
"a!b" /* ERROR invalid import path */ /* ERROR invalid import path */
"abc\xffdef" /* ERROR invalid import path */ /* ERROR invalid import path */
)
// using "math" in this file doesn't affect its use in other files
const Pi0 = math.Pi
const Pi1 = m.Pi