Skip to content

Commit

Permalink
Resolve underlying type to detect overflows in type aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
gartnera authored and ccojocar committed Jul 20, 2024
1 parent 4487a0c commit 08b94f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 2 additions & 2 deletions analyzers/conversion_overflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func runConversionOverflow(pass *analysis.Pass) (interface{}, error) {
for _, instr := range block.Instrs {
switch instr := instr.(type) {
case *ssa.Convert:
src := instr.X.Type().String()
dst := instr.Type().String()
src := instr.X.Type().Underlying().String()
dst := instr.Type().Underlying().String()
if isIntOverflow(src, dst) {
issue := newIssue(pass.Analyzer.Name,
fmt.Sprintf("integer overflow conversion %s -> %s", src, dst),
Expand Down
36 changes: 36 additions & 0 deletions testutils/g115_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,40 @@ func ExampleFunction() {
}
`,
}, 0, gosec.NewConfig()},
{[]string{
`
package main
import (
"fmt"
"math"
)
type Uint uint
func main() {
var a uint8 = math.MaxUint8
b := Uint(a)
fmt.Println(b)
}
`,
}, 0, gosec.NewConfig()},
{[]string{
`
package main
import (
"fmt"
"math"
)
type CustomType int
func main() {
var a uint = math.MaxUint
b := CustomType(a)
fmt.Println(b)
}
`,
}, 1, gosec.NewConfig()},
}

0 comments on commit 08b94f9

Please sign in to comment.