1
0
mirror of https://github.com/securego/gosec.git synced 2025-07-17 01:12:33 +02:00

Ignore struct pointers in G601 (#1003)

Updates https://github.com/securego/gosec/issues/966

Signed-off-by: Alexander Yastrebov <yastrebov.alex@gmail.com>
This commit is contained in:
Alexander Yastrebov
2023-08-18 17:05:17 +02:00
committed by GitHub
parent 85005c43d9
commit 21d13c9a9b
3 changed files with 100 additions and 21 deletions

View File

@ -3,6 +3,7 @@ package rules
import (
"go/ast"
"go/token"
"go/types"
"github.com/securego/gosec/v2"
"github.com/securego/gosec/v2/issue"
@ -28,23 +29,20 @@ func containsUnary(exprs []*ast.UnaryExpr, expr *ast.UnaryExpr) bool {
return false
}
func getIdentExpr(expr ast.Expr) *ast.Ident {
func getIdentExpr(expr ast.Expr) (*ast.Ident, bool) {
return doGetIdentExpr(expr, false)
}
func doGetIdentExpr(expr ast.Expr, hasSelector bool) (*ast.Ident, bool) {
switch node := expr.(type) {
case *ast.Ident:
return node
return node, hasSelector
case *ast.SelectorExpr:
return getIdentExpr(node.X)
return doGetIdentExpr(node.X, true)
case *ast.UnaryExpr:
switch e := node.X.(type) {
case *ast.Ident:
return e
case *ast.SelectorExpr:
return getIdentExpr(e.X)
default:
return nil
}
return doGetIdentExpr(node.X, hasSelector)
default:
return nil
return nil, false
}
}
@ -92,9 +90,13 @@ func (r *implicitAliasing) Match(n ast.Node, c *gosec.Context) (*issue.Issue, er
}
// If we find a unary op of & (reference) of an object within r.aliases, complain.
if identExpr := getIdentExpr(node); identExpr != nil && node.Op.String() == "&" {
if identExpr, hasSelector := getIdentExpr(node); identExpr != nil && node.Op.String() == "&" {
if _, contains := r.aliases[identExpr.Obj]; contains {
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
_, isPointer := c.Info.TypeOf(identExpr).(*types.Pointer)
if !hasSelector || !isPointer {
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil
}
}
}
case *ast.ReturnStmt: