Skip to content

Commit 5b580c7

Browse files
authored
Fix G602 regression coverage for issue #1545 and stabilize G117 TOML test dependency (#1546)
This PR adds regression coverage for the G602 false-positive reported in issue #1545 by introducing two sample cases: one valid range-over-array indexing pattern that should not trigger, and one true out-of-bounds variant that should still be detected. It also fixes test instability in the rules suite by adding the missing BurntSushi TOML module metadata required by G117 sample compilation in the test harness. Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
1 parent eba2d15 commit 5b580c7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
cloud.google.com/go v0.121.2 // indirect
2525
cloud.google.com/go/auth v0.16.5 // indirect
2626
cloud.google.com/go/compute/metadata v0.8.0 // indirect
27+
github.com/BurntSushi/toml v0.3.1 // indirect
2728
github.com/Masterminds/semver/v3 v3.4.0 // indirect
2829
github.com/davecgh/go-spew v1.1.1 // indirect
2930
github.com/felixge/httpsnoop v1.0.4 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
4040
contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EUaVTwzvYETVNZk10Pu26tevFKLUc=
4141
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
4242
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
43+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
4344
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
4445
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
4546
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=

testutils/g602_samples.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,5 +714,30 @@ func main() {
714714
_ = arr[i+1]
715715
}
716716
}
717+
`}, 1, gosec.NewConfig()},
718+
// Issue #1545: G602 false positive on range-over-array indexing into same-size array
719+
{[]string{`
720+
package main
721+
722+
func main() {
723+
ranged := [1]int{1}
724+
var accessed [1]*int
725+
726+
for i, r := range ranged {
727+
accessed[i] = &r
728+
}
729+
}
730+
`}, 0, gosec.NewConfig()},
731+
{[]string{`
732+
package main
733+
734+
func main() {
735+
ranged := [2]int{1, 2}
736+
var accessed [1]*int
737+
738+
for i, r := range ranged {
739+
accessed[i] = &r
740+
}
741+
}
717742
`}, 1, gosec.NewConfig()},
718743
}

0 commit comments

Comments
 (0)