mirror of
https://github.com/securego/gosec.git
synced 2024-11-05 11:35:51 +00:00
Handle new function when getting the call info in case is overriden
Signed-off-by: Cosmin Cojocar <gcojocar@adobe.com>
This commit is contained in:
parent
5b7867d125
commit
a11eb28e2f
2 changed files with 33 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue