Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go/internal/load: prevent calling ImportErrorf when the err is *module.InvalidPathError #50089

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,12 +1857,26 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
if other := foldPath[fold]; other == "" {
foldPath[fold] = p.ImportPath
} else if other != p.ImportPath {
setError(ImportErrorf(p.ImportPath, "case-insensitive import collision: %q and %q", p.ImportPath, other))
// It is unnecessary to set an ImportPathError when the error is already
// a module.InvalidPathError.
// Beside, ImportErrorf may panic when the import path is invalid.
// See https://golang.org/issue/49137
var invalidPathErr *module.InvalidPathError
if !errors.As(err, &invalidPathErr) {
setError(ImportErrorf(p.ImportPath, "case-insensitive import collision: %q and %q", p.ImportPath, other))
}
return
}

if !SafeArg(p.ImportPath) {
setError(ImportErrorf(p.ImportPath, "invalid import path %q", p.ImportPath))
// It is unnecessary to set an ImportPathError when the error is already
// a module.InvalidPathError.
// Beside, ImportErrorf may panic when the import path is invalid.
// See https://golang.org/issue/49137
var invalidPathErr *module.InvalidPathError
if !errors.As(err, &invalidPathErr) {
setError(ImportErrorf(p.ImportPath, "invalid import path %q", p.ImportPath))
}
return
}

Expand Down
14 changes: 14 additions & 0 deletions src/cmd/go/testdata/script/load_invalid_import_path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Tests issue 49137. The following commands should not panic any way.

! go list '"a"'
stderr '^malformed import path "\\"a\\"": invalid char ''"''$'

! go build '' -ldflags='-X "main.Tags="'
stderr '^malformed import path "-ldflags=-X \\"main.Tags=\\"": leading dash$'

-- go.mod --
module m

go 1.16
-- main.go --
package main