Skip to content

Commit

Permalink
Handle new function when getting the call info in case is overriden
Browse files Browse the repository at this point in the history
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
  • Loading branch information
Cosmin Cojocar authored and ccojocar committed Oct 12, 2023
1 parent 5b7867d commit a11eb28
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion helpers.go
Expand Up @@ -183,7 +183,7 @@ func GetCallInfo(n ast.Node, ctx *Context) (string, string, error) {
case *ast.CallExpr:
switch call := expr.Fun.(type) {
case *ast.Ident:
if call.Name == "new" {
if call.Name == "new" && len(expr.Args) > 0 {
t := ctx.Info.TypeOf(expr.Args[0])
if t != nil {
return t.String(), fn.Sel.Name, nil
Expand Down
32 changes: 32 additions & 0 deletions helpers_test.go
Expand Up @@ -251,6 +251,38 @@ var _ = Describe("Helpers", func() {

Expect(result).Should(HaveKeyWithValue("fmt", "Println"))
})

It("should return the type and call name when built-in new function is overriden", func() {
pkg := testutils.NewTestPackage()
defer pkg.Close()
pkg.AddFile("main.go", `
package main
type S struct{ F int }
func (f S) Fun() {}
func new() S { return S{} }
func main() {
new().Fun()
}
`)
ctx := pkg.CreateContext("main.go")
result := map[string]string{}
visitor := testutils.NewMockVisitor()
visitor.Context = ctx
visitor.Callback = func(n ast.Node, ctx *gosec.Context) bool {
typeName, call, err := gosec.GetCallInfo(n, ctx)
if err == nil {
result[typeName] = call
}
return true
}
ast.Walk(visitor, ctx.Root)

Expect(result).Should(HaveKeyWithValue("main", "new"))
})
})
Context("when getting binary expression operands", func() {
It("should return all operands of a binary expression", func() {
Expand Down

0 comments on commit a11eb28

Please sign in to comment.