Skip to content

Commit a7ec9cc

Browse files
committed
Backport test case for 1.5
Go 1.5 does not have a rand.Read function so need to adjust test definitions accordingly.
1 parent f9868aa commit a7ec9cc

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

rules/rand.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,23 @@ import (
2222

2323
type WeakRand struct {
2424
gas.MetaData
25-
funcName string
25+
funcNames []string
2626
packagePath string
2727
}
2828

2929
func (w *WeakRand) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
30-
if _, matched := gas.MatchCallByPackage(n, c, w.packagePath, w.funcName); matched {
31-
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
30+
for _, funcName := range w.funcNames {
31+
if _, matched := gas.MatchCallByPackage(n, c, w.packagePath, funcName); matched {
32+
return gas.NewIssue(c, n, w.What, w.Severity, w.Confidence), nil
33+
}
3234
}
3335

3436
return nil, nil
3537
}
3638

3739
func NewWeakRandCheck(conf map[string]interface{}) (gas.Rule, []ast.Node) {
3840
return &WeakRand{
39-
funcName: "Read",
41+
funcNames: []string{"Read", "Int"},
4042
packagePath: "math/rand",
4143
MetaData: gas.MetaData{
4244
Severity: gas.High,

rules/rand_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestRandBad(t *testing.T) {
5151
import "math/rand"
5252
5353
func main() {
54-
bad, _ := rand.Read(nil)
54+
bad := rand.Int()
5555
println(bad)
5656
5757
}`, analyzer)
@@ -77,7 +77,7 @@ func TestRandRenamed(t *testing.T) {
7777
func main() {
7878
good, _ := rand.Read(nil)
7979
println(good)
80-
i := mrand.Int()
80+
i := mrand.Int31()
8181
println(i)
8282
}`, analyzer)
8383

0 commit comments

Comments
 (0)