Skip to content

Commit 602ced7

Browse files
authored
Fix wrong location for G109 (#829)
Before this commit, G109 will report on `strconv.Atoi`. After this, it will report on the convertion like`int32(a)`.
1 parent 7dd9ddd commit 602ced7

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

rules/integer_overflow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (i *integerOverflowCheck) Match(node ast.Node, ctx *gosec.Context) (*gosec.
6161
if fun, ok := n.Fun.(*ast.Ident); ok {
6262
if fun.Name == "int32" || fun.Name == "int16" {
6363
if idt, ok := n.Args[0].(*ast.Ident); ok {
64-
if n, ok := atoiVarObj[idt.Obj]; ok {
64+
if _, ok := atoiVarObj[idt.Obj]; ok {
6565
// Detect int32(v) and int16(v)
6666
return gosec.NewIssue(ctx, n, i.ID(), i.What, i.Severity, i.Confidence), nil
6767
}

testutils/source.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ func main() {
795795
}
796796
value := int32(bigValue)
797797
fmt.Println(value)
798-
}`}, 1, gosec.NewConfig()}, {[]string{`
798+
}`}, 1, gosec.NewConfig()},
799+
{[]string{`
799800
package main
800801
801802
import (
@@ -811,7 +812,8 @@ func main() {
811812
if int16(bigValue) < 0 {
812813
fmt.Println(bigValue)
813814
}
814-
}`}, 1, gosec.NewConfig()}, {[]string{`
815+
}`}, 1, gosec.NewConfig()},
816+
{[]string{`
815817
package main
816818
817819
import (
@@ -825,7 +827,8 @@ func main() {
825827
panic(err)
826828
}
827829
fmt.Println(bigValue)
828-
}`}, 0, gosec.NewConfig()}, {[]string{`
830+
}`}, 0, gosec.NewConfig()},
831+
{[]string{`
829832
package main
830833
831834
import (
@@ -846,7 +849,8 @@ func test() {
846849
bigValue := 30
847850
value := int32(bigValue)
848851
fmt.Println(value)
849-
}`}, 0, gosec.NewConfig()}, {[]string{`
852+
}`}, 0, gosec.NewConfig()},
853+
{[]string{`
850854
package main
851855
852856
import (
@@ -862,6 +866,17 @@ func main() {
862866
}
863867
v := int32(value)
864868
fmt.Println(v)
869+
}`}, 0, gosec.NewConfig()},
870+
{[]string{`
871+
package main
872+
import (
873+
"fmt"
874+
"strconv"
875+
)
876+
func main() {
877+
a, err := strconv.Atoi("a")
878+
b := int32(a) //#nosec G109
879+
fmt.Println(b, err)
865880
}`}, 0, gosec.NewConfig()},
866881
}
867882

0 commit comments

Comments
 (0)