Skip to content

Commit 7da9f46

Browse files
ccojocarCosmin Cojocar
authored andcommitted
Fix the call list info to handle selector expressions
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent cf25904 commit 7da9f46

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

call_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var _ = Describe("Call List", func() {
9999
// Create file to be scanned
100100
pkg := testutils.NewTestPackage()
101101
defer pkg.Close()
102-
pkg.AddFile("main.go", testutils.SampleCodeG104[5].Code[0])
102+
pkg.AddFile("main.go", testutils.SampleCodeG104[6].Code[0])
103103

104104
ctx := pkg.CreateContext("main.go")
105105

helpers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
135135
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
136136
}
137137
return expr.Name, fn.Sel.Name, nil
138+
case *ast.SelectorExpr:
139+
if expr.Sel != nil {
140+
t := ctx.Info.TypeOf(expr.Sel)
141+
if t != nil {
142+
return t.String(), fn.Sel.Name, nil
143+
}
144+
return "undefined", fn.Sel.Name, fmt.Errorf("missing type info")
145+
}
138146
case *ast.CallExpr:
139147
switch call := expr.Fun.(type) {
140148
case *ast.Ident:

testutils/source.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,23 @@ package main
234234
func dummy(){}
235235
`}, 0, gosec.NewConfig()}, {[]string{`
236236
package main
237+
238+
import (
239+
"bytes"
240+
)
241+
242+
type a struct {
243+
buf *bytes.Buffer
244+
}
245+
246+
func main() {
247+
a := &a{
248+
buf: new(bytes.Buffer),
249+
}
250+
a.buf.Write([]byte{0})
251+
}
252+
`}, 0, gosec.NewConfig()}, {[]string{`
253+
package main
237254
import (
238255
"io/ioutil"
239256
"os"

0 commit comments

Comments
 (0)