From: Michael Matloob Date: Tue, 6 Aug 2024 16:42:34 +0000 (-0400) Subject: cmd/go/internal/load: make check for path in import error more robust X-Git-Tag: go1.24rc1~1265 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=44ed517c42a52e0c5831383b59eae5b38428656d;p=gostls13.git cmd/go/internal/load: make check for path in import error more robust When producing an ImportPathError from ImportErrorf, we check to see whether the error string contains the path for the error. The issue is that we were checking for the exact path string when sometimes the string is quoted when the error is constructed, and the escaping in the quote may not match the path string. Check for both the path string, and the quoted path string. Fixes #68737 Change-Id: I01bf4e495056e929570bc11bc1f2000ce6d2802b Reviewed-on: https://go-review.googlesource.com/c/go/+/603475 LUCI-TryBot-Result: Go LUCI Reviewed-by: Sam Thanawalla --- diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 238fb6efd2..33bc3e0c48 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -539,7 +539,7 @@ type importError struct { func ImportErrorf(path, format string, args ...any) ImportPathError { err := &importError{importPath: path, err: fmt.Errorf(format, args...)} - if errStr := err.Error(); !strings.Contains(errStr, path) { + if errStr := err.Error(); !strings.Contains(errStr, path) && !strings.Contains(errStr, strconv.Quote(path)) { panic(fmt.Sprintf("path %q not in error %q", path, errStr)) } return err diff --git a/src/cmd/go/testdata/script/list_panic_issue68737.txt b/src/cmd/go/testdata/script/list_panic_issue68737.txt new file mode 100644 index 0000000000..db059c8fed --- /dev/null +++ b/src/cmd/go/testdata/script/list_panic_issue68737.txt @@ -0,0 +1,7 @@ +# Issue #68737: Don't panic if the import path string doesn't appear +# in the import error. The string may not appear because it may be +# escaped when quoted as part of the error message. + +! go run '' # Quote contains 0x01 byte +! stderr panic +stderr 'malformed import path "\\x01": invalid char ''\\x01'''