]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/load: make check for path in import error more robust
authorMichael Matloob <matloob@golang.org>
Tue, 6 Aug 2024 16:42:34 +0000 (12:42 -0400)
committerMichael Matloob <matloob@golang.org>
Tue, 6 Aug 2024 20:20:34 +0000 (20:20 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_panic_issue68737.txt [new file with mode: 0644]

index 238fb6efd222e2ce0d37dcdf79ba57a84ff0dce4..33bc3e0c48778cf352ccc3bfa92b2f24a9fb7bf5 100644 (file)
@@ -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 (file)
index 0000000..db059c8
--- /dev/null
@@ -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 '\ 1' # Quote contains 0x01 byte
+! stderr panic
+stderr 'malformed import path "\\x01": invalid char ''\\x01'''