Skip to content

Commit 3d35eff

Browse files
DanielMorsinggopherbot
authored andcommitted
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 <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Madeline Kalil <mkalil@google.com> Auto-Submit: Alan Donovan <adonovan@google.com>
1 parent 985c282 commit 3d35eff

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

go/analysis/passes/modernize/stringscut.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ func indexArgValid(info *types.Info, index *typeindex.Index, expr ast.Expr, afte
345345
switch expr := expr.(type) {
346346
case *ast.CallExpr:
347347
return types.Identical(tv.Type, byteSliceType) &&
348+
info.Types[expr.Fun].IsType() && // make sure this isn't a function that returns a byte slice
348349
indexArgValid(info, index, expr.Args[0], afterPos) // check s in []byte(s)
349350
case *ast.Ident:
350351
sObj := info.Uses[expr]

go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ func idx_gtr_negone(s string, sub string) string {
289289
return ""
290290
}
291291

292+
// Regression test for a crash (https://go.dev/issue/77208)
293+
func idx_call() {
294+
i := bytes.Index(b(), []byte(""))
295+
_ = i
296+
}
297+
298+
func b() []byte {
299+
return nil
300+
}
301+
292302
func function(s string) {}
293303

294304
func reference_str(s *string) {}

go/analysis/passes/modernize/testdata/src/stringscut/stringscut.go.golden

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,16 @@ func idx_gtr_negone(s string, sub string) string {
289289
return ""
290290
}
291291

292+
// Regression test for a crash (https://go.dev/issue/77208)
293+
func idx_call() {
294+
i := bytes.Index(b(), []byte(""))
295+
_ = i
296+
}
297+
298+
func b() []byte {
299+
return nil
300+
}
301+
292302
func function(s string) {}
293303

294304
func reference_str(s *string) {}

0 commit comments

Comments
 (0)