Skip to content

Commit 5b71c2b

Browse files
author
Cosmin Cojocar
committed
Add a test for math/big.Int.Exp rule
1 parent 65b8e74 commit 5b71c2b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

rules/big.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ type UsingBigExp struct {
2626
}
2727

2828
func (r *UsingBigExp) Match(n ast.Node, c *gas.Context) (gi *gas.Issue, err error) {
29-
if _, matches := gas.MatchCallByPackage(n, c, r.pkg, r.calls...); matches {
29+
if _, matched := gas.MatchCallByType(n, c, r.pkg, r.calls...); matched {
3030
return gas.NewIssue(c, n, r.What, r.Severity, r.Confidence), nil
3131
}
3232
return nil, nil
3333
}
3434
func NewUsingBigExp(conf map[string]interface{}) (gas.Rule, []ast.Node) {
3535
return &UsingBigExp{
36-
pkg: "big",
36+
pkg: "*math/big.Int",
3737
calls: []string{"Exp"},
3838
MetaData: gas.MetaData{
39-
What: "Use of big.Exp call should be audited for modulus == 0",
39+
What: "Use of math/big.Int.Exp function should be audited for modulus == 0",
4040
Severity: gas.Low,
4141
Confidence: gas.High,
4242
},

rules/big_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package rules
16+
17+
import (
18+
"testing"
19+
20+
gas "github.com/GoASTScanner/gas/core"
21+
)
22+
23+
func TestBigExp(t *testing.T) {
24+
config := map[string]interface{}{"ignoreNosec": false}
25+
analyzer := gas.NewAnalyzer(config, nil)
26+
analyzer.AddRule(NewUsingBigExp(config))
27+
28+
issues := gasTestRunner(`
29+
package main
30+
31+
import (
32+
"math/big"
33+
)
34+
35+
func main() {
36+
z := new(big.Int)
37+
x := new(big.Int)
38+
x = x.SetUint64(2)
39+
y := new(big.Int)
40+
y = y.SetUint64(4)
41+
m := new(big.Int)
42+
m = m.SetUint64(0)
43+
44+
z = z.Exp(x, y, m)
45+
}
46+
`, analyzer)
47+
48+
checkTestResults(t, issues, 1, "Use of math/big.Int.Exp function")
49+
}

0 commit comments

Comments
 (0)