Skip to content

Commit

Permalink
Fix conversion overflow false positive when using ParseUint
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-krieger authored and ccojocar committed Aug 28, 2024
1 parent c52dc0e commit 4ae73c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 3 additions & 2 deletions analyzers/conversion_overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ func isStringToIntConversion(instr *ssa.Convert, dstType string) bool {
for {
switch v := original.(type) {
case *ssa.Call:
if v.Call.StaticCallee() != nil && v.Call.StaticCallee().Name() == "ParseInt" {
if v.Call.StaticCallee() != nil && (v.Call.StaticCallee().Name() == "ParseInt" || v.Call.StaticCallee().Name() == "ParseUint") {
if len(v.Call.Args) == 3 {
if bitSize, ok := v.Call.Args[2].(*ssa.Const); ok {
signed := v.Call.StaticCallee().Name() == "ParseInt"
bitSizeValue, err := strconv.Atoi(bitSize.Value.String())
if err != nil {
return false
Expand All @@ -145,7 +146,7 @@ func isStringToIntConversion(instr *ssa.Convert, dstType string) bool {
if err != nil {
return false
}
isSafe := bitSizeValue <= dstInt.size
isSafe := bitSizeValue <= dstInt.size && signed == dstInt.signed
return isSafe
}
}
Expand Down
34 changes: 34 additions & 0 deletions testutils/g115_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,38 @@ func main() {
}
`,
}, 0, gosec.NewConfig()},
{[]string{
`
package main
import (
"fmt"
"strconv"
)
func main() {
var a string = "13"
b, _ := strconv.ParseUint(a, 10, 8)
c := uint8(b)
fmt.Printf("%d\n", c)
}
`,
}, 0, gosec.NewConfig()},
{[]string{
`
package main
import (
"fmt"
"strconv"
)
func main() {
var a string = "13"
b, _ := strconv.ParseInt(a, 10, 8)
c := uint8(b)
fmt.Printf("%d\n", c)
}
`,
}, 1, gosec.NewConfig()},
}

0 comments on commit 4ae73c8

Please sign in to comment.