go/analysis/passes/modernize: fix panic in stringscut
stringscut modernizer assumes that any CallExpr that occurs in the first
argument is a []byte(string) conversion, causing panics when given a function
call with no arguments at all.
Fixes golang/go#77208.
diff --git a/go/analysis/passes/modernize/stringscut.go b/go/analysis/passes/modernize/stringscut.go
index 08ce2a8..3835a10 100644
--- a/go/analysis/passes/modernize/stringscut.go
+++ b/go/analysis/passes/modernize/stringscut.go
@@ -345,6 +345,7 @@
switch expr := expr.(type) {
case *ast.CallExpr:
return types.Identical(tv.Type, byteSliceType) &&
+ types.Identical(info.Types[expr.Fun].Type, byteSliceType) && // make sure this isn't a function that returns a byte slice
indexArgValid(info, index, expr.Args[0], afterPos) // check s in []byte(s)
case *ast.Ident:
sObj := info.Uses[expr]
diff --git a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
index 0390eab..9500b08 100644
--- a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
+++ b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
@@ -289,6 +289,15 @@
return ""
}
+func idx_call() {
+ i := bytes.Index(b(), []byte(""))
+ _ = i
+}
+
+func b() []byte {
+ return nil
+}
+
func function(s string) {}
func reference_str(s *string) {}
diff --git a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
index a308fb2..2c3d81e 100644
--- a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
@@ -289,6 +289,15 @@
return ""
}
+func idx_call() {
+ i := bytes.Index(b(), []byte(""))
+ _ = i
+}
+
+func b() []byte {
+ return nil
+}
+
func function(s string) {}
func reference_str(s *string) {}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
types.Identical(info.Types[expr.Fun].Type, byteSliceType) && // make sure this isn't a function that returns a byte sliceA more direct check is `info.Types[expr.Fun].IsType()`, which reports whether this is a conversion without caring about the specific types involved.
func idx_call() {| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Commit-Queue | +1 |
types.Identical(info.Types[expr.Fun].Type, byteSliceType) && // make sure this isn't a function that returns a byte sliceA more direct check is `info.Types[expr.Fun].IsType()`, which reports whether this is a conversion without caring about the specific types involved.
Done
Explain the intent:
// Regression test for a crash (go.dev/issue/77208).
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Auto-Submit | +1 |
| Code-Review | +2 |
| Commit-Queue | +1 |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
The test failure is spurious; I've filed https://github.com/golang/go/issues/77211.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Code-Review | +2 |
| Commit-Queue | +1 |
Thank you!
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
go/analysis/passes/modernize: fix panic in stringscut
stringscut modernizer assumes that any CallExpr that occurs in the first
argument is a []byte(string) conversion, causing panics when given a
function call with no arguments at all.
Fixes golang/go#77208.
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
[internal-branch.go1.26-vendor] go/analysis/passes/modernize: fix panic in stringscut
stringscut modernizer assumes that any CallExpr that occurs in the first
argument is a []byte(string) conversion, causing panics when given a
function call with no arguments at all.
Fixes golang/go#77208.
Change-Id: I17401c74c4681b24fb3a508cafafffee6a6a6964
Reviewed-on: https://go-review.googlesource.com/c/tools/+/737000
Reviewed-by: Alan Donovan <adon...@google.com>
LUCI-TryBot-Result: Go LUCI <golang...@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Madeline Kalil <mka...@google.com>
Auto-Submit: Alan Donovan <adon...@google.com>
(cherry picked from commit 3d35eff1a742037163a41b9e4b2ec9977618d1f8)
diff --git a/go/analysis/passes/modernize/stringscut.go b/go/analysis/passes/modernize/stringscut.go
index 08ce2a8..62088f0 100644
--- a/go/analysis/passes/modernize/stringscut.go
+++ b/go/analysis/passes/modernize/stringscut.go
@@ -345,6 +345,7 @@
switch expr := expr.(type) {
case *ast.CallExpr:
return types.Identical(tv.Type, byteSliceType) &&
+ info.Types[expr.Fun].IsType() && // make sure this isn't a function that returns a byte slice
indexArgValid(info, index, expr.Args[0], afterPos) // check s in []byte(s)
case *ast.Ident:
sObj := info.Uses[expr]
diff --git a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
index 0390eab..5990771 100644
--- a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
+++ b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go
@@ -289,6 +289,16 @@
return ""
}
+// Regression test for a crash (https://go.dev/issue/77208)
+func idx_call() {
+ i := bytes.Index(b(), []byte(""))
+ _ = i
+}
+
+func b() []byte {
+ return nil
+}
+
func function(s string) {}
func reference_str(s *string) {}
diff --git a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
index a308fb2..e274382 100644
--- a/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
+++ b/go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden
@@ -289,6 +289,16 @@
return ""
}
+// Regression test for a crash (https://go.dev/issue/77208)
+func idx_call() {
+ i := bytes.Index(b(), []byte(""))
+ _ = i
+}
+
+func b() []byte {
+ return nil
+}
+
func function(s string) {}
func reference_str(s *string) {}
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |
| Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. |