Fix the call list info to handle selector expressions

Signed-off-by: Cosmin Cojocar <cosmin.cojocar@gmx.ch>
This commit is contained in:
Cosmin Cojocar 2020-03-15 15:42:26 +01:00 committed by Cosmin Cojocar
parent cf2590442c
commit 7da9f46445
3 changed files with 26 additions and 1 deletions

View file

@ -99,7 +99,7 @@ var _ = Describe("Call List", func() {
// Create file to be scanned
pkg := testutils.NewTestPackage()
defer pkg.Close()
pkg.AddFile("main.go", testutils.SampleCodeG104[5].Code[0])
pkg.AddFile("main.go", testutils.SampleCodeG104[6].Code[0])
ctx := pkg.CreateContext("main.go")

View file

@ -135,6 +135,14 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
return expr.Name, fn.Sel.Name, nil
case *ast.SelectorExpr:
if expr.Sel != nil {
t := ctx.Info.TypeOf(expr.Sel)
if t != nil {
return t.String(), fn.Sel.Name, nil
}
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
}
case *ast.CallExpr:
switch call := expr.Fun.(type) {
case *ast.Ident:

View file

@ -234,6 +234,23 @@ package main
func dummy(){}
`}, 0, gosec.NewConfig()}, {[]string{`
package main
import (
"bytes"
)
type a struct {
buf *bytes.Buffer
}
func main() {
a := &a{
buf: new(bytes.Buffer),
}
a.buf.Write([]byte{0})
}
`}, 0, gosec.NewConfig()}, {[]string{`
package main
import (
"io/ioutil"
"os"