Skip to content

Commit a11eb28

Browse files
committed
Handle new function when getting the call info in case is overriden
Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 5b7867d commit a11eb28

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
183183
case *ast.CallExpr:
184184
switch call := expr.Fun.(type) {
185185
case *ast.Ident:
186-
if call.Name == "new" {
186+
if call.Name == "new" && len(expr.Args) > 0 {
187187
t := ctx.Info.TypeOf(expr.Args[0])
188188
if t != nil {
189189
return t.String(), fn.Sel.Name, nil

helpers_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,38 @@ var _ = Describe("Helpers", func() {
251251

252252
Expect(result).Should(HaveKeyWithValue("fmt", "Println"))
253253
})
254+
255+
It("should return the type and call name when built-in new function is overriden", func() {
256+
pkg := testutils.NewTestPackage()
257+
defer pkg.Close()
258+
pkg.AddFile("main.go", `
259+
package main
260+
261+
type S struct{ F int }
262+
263+
func (f S) Fun() {}
264+
265+
func new() S { return S{} }
266+
267+
func main() {
268+
new().Fun()
269+
}
270+
`)
271+
ctx := pkg.CreateContext("main.go")
272+
result := map[string]string{}
273+
visitor := testutils.NewMockVisitor()
274+
visitor.Context = ctx
275+
visitor.Callback = func(n ast.Node, ctx *gosec.Context) bool {
276+
typeName, call, err := gosec.GetCallInfo(n, ctx)
277+
if err == nil {
278+
result[typeName] = call
279+
}
280+
return true
281+
}
282+
ast.Walk(visitor, ctx.Root)
283+
284+
Expect(result).Should(HaveKeyWithValue("main", "new"))
285+
})
254286
})
255287
Context("when getting binary expression operands", func() {
256288
It("should return all operands of a binary expression", func() {

0 commit comments

Comments
 (0)