Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix G115 false positive when going from parsed uint to larger int #1254

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion analyzers/conversion_overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ func isStringToIntConversion(instr *ssa.Convert, dstType string) bool {
if err != nil {
return false
}
isSafe := bitSizeValue <= dstInt.size && signed == dstInt.signed

// we're good if:
// - signs match and bit size is <= than destination
// - parsing unsigned and bit size is < than destination
isSafe := (bitSizeValue <= dstInt.size && signed == dstInt.signed) ||
(bitSizeValue < dstInt.size && !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 @@ -426,6 +426,40 @@ import (
"strconv"
)

func main() {
var a string = "13"
b, _ := strconv.ParseUint(a, 10, 16)
c := int(b)
fmt.Printf("%d\n", c)
}
`,
}, 0, gosec.NewConfig()},
{[]string{
`
package main

import (
"fmt"
"strconv"
)

func main() {
var a string = "13"
b, _ := strconv.ParseUint(a, 10, 31)
c := int32(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)
Expand Down
Loading