Skip to content

Commit

Permalink
fix: report when a UnaryExpr is used in a CompositeLit directly
Browse files Browse the repository at this point in the history
closes #106
  • Loading branch information
kalexmills committed Dec 26, 2020
1 parent ed605dd commit 4f4a1da
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
48 changes: 33 additions & 15 deletions cmd/vet-bot/looppointer/looppointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
ReasonCallMaybeAsync
// ReasonCallPassesToThirdParty indicates some function call passes a pointer to third-party code
ReasonCallPassesToThirdParty
// ReasonPointerStoredInCompositeLit indicates a pointer to a range-loop variable was used in a composite literal.
ReasonPointerStoredInCompositeLit
)

// Message returns a human-readable message, provided the name of the varaible and
Expand All @@ -80,6 +82,8 @@ func (r Reason) Message(name string, pos token.Position) string {
return fmt.Sprintf("function call which takes a reference to %s at line %d may start a goroutine", name, pos.Line)
case ReasonCallPassesToThirdParty:
return fmt.Sprintf("function call at line %d passes reference to %s to third-party code", pos.Line, name)
case ReasonPointerStoredInCompositeLit:
return fmt.Sprintf("reference to %s was used in a composite literal at line %d", name, pos.Line)
default:
return ""
}
Expand Down Expand Up @@ -123,9 +127,9 @@ func (s *Searcher) innermostLoop(stack []ast.Node) *ast.RangeStmt {
return nil
}

// assignStmt returns the most recent assign statement, along with the child expression
// innermostAssignStmt returns the most recent assign statement, along with the child expression
// from the stack.
func (s *Searcher) assignStmt(stack []ast.Node) (*ast.AssignStmt, ast.Node) {
func innermostAssignStmt(stack []ast.Node) (*ast.AssignStmt, ast.Node) {
for i := len(stack) - 1; i >= 0; i-- {
if typed, ok := stack[i].(*ast.AssignStmt); ok {
return typed, stack[i+1]
Expand All @@ -134,7 +138,7 @@ func (s *Searcher) assignStmt(stack []ast.Node) (*ast.AssignStmt, ast.Node) {
return nil, nil
}

func (s *Searcher) callExpr(stack []ast.Node) *ast.CallExpr {
func innermostCallExpr(stack []ast.Node) *ast.CallExpr {
for i := len(stack) - 1; i >= 0; i-- {
if typed, ok := stack[i].(*ast.CallExpr); ok {
return typed
Expand All @@ -143,6 +147,15 @@ func (s *Searcher) callExpr(stack []ast.Node) *ast.CallExpr {
return nil
}

func innermostCompositeLit(stack []ast.Node) *ast.CompositeLit {
for i := len(stack) - 1; i >= 0; i-- {
if typed, ok := stack[i].(*ast.CompositeLit); ok {
return typed
}
}
return nil
}

func (s *Searcher) checkUnaryExpr(n *ast.UnaryExpr, stack []ast.Node, pass *analysis.Pass) Reason {
if n.Op != token.AND {
return ReasonNone
Expand All @@ -166,15 +179,18 @@ func (s *Searcher) checkUnaryExpr(n *ast.UnaryExpr, stack []ast.Node, pass *anal
}
rangeLoop := s.Stats[id.Obj.Pos()]

// If the identity is not used in an assignment or call expression, it
// will not be reported.
assignStmt, child := s.assignStmt(stack)
callExpr := s.callExpr(stack)
if assignStmt == nil && callExpr == nil {
return ReasonNone
// TODO: encapsulate the following cases into separate functions to make the logic clearer.

// TODO: If the identity is used in a CompositeLit directly, we must report that.
compositeLit := innermostCompositeLit(stack)
if compositeLit != nil {
reportBasic(pass, rangeLoop, ReasonPointerStoredInCompositeLit, id)
return ReasonPointerStoredInCompositeLit
}

// TODO: encapsulate the two following cases into separate functions to make the logic here clearer.
// If the identity is not used in an assignment, call expression, or composite literal it
// will not be reported.
assignStmt, child := innermostAssignStmt(stack)

// If the identity is used in an AssignStmt directly, we must report that.
if assignStmt != nil {
Expand All @@ -186,15 +202,17 @@ func (s *Searcher) checkUnaryExpr(n *ast.UnaryExpr, stack []ast.Node, pass *anal
}
for _, expr := range assignStmt.Rhs {
if expr.Pos() == child.Pos() && child.Pos() == n.Pos() {
reportBasic(pass, rangeLoop, ReasonPointerReassigned, n, id)
reportBasic(pass, rangeLoop, ReasonPointerReassigned, id)
return ReasonPointerReassigned
}
}
return ReasonNone
}

// TODO: If the identity is used in a CompositeLit directly, we must report that.

callExpr := innermostCallExpr(stack)
if callExpr == nil {
return ReasonNone
}
// unaryExpr occurred in a CallExpr
if len(callExpr.Args) == 0 {
log.Println("sanity check failed: UnaryExpr 'occurred' inside a CallExpr with zero arguments")
Expand Down Expand Up @@ -356,14 +374,14 @@ func reportPathGraph(pathGraph map[string]map[string]struct{}, badFuncPhrase str
}

// TODO: remove this function and make it more specific....
func reportBasic(pass *analysis.Pass, rangeLoop *ast.RangeStmt, reason Reason, n ast.Node, id *ast.Ident) {
func reportBasic(pass *analysis.Pass, rangeLoop *ast.RangeStmt, reason Reason, id *ast.Ident) {
pass.Report(analysis.Diagnostic{
Pos: rangeLoop.Pos(),
End: rangeLoop.End(),
Message: reason.Message(id.Name, pass.Fset.Position(id.Pos())),

Related: []analysis.RelatedInformation{
{Message: pass.Fset.File(n.Pos()).Name()},
{Message: pass.Fset.File(id.Pos()).Name()},
},
})
}
Expand Down
22 changes: 21 additions & 1 deletion cmd/vet-bot/looppointer/testdata/src/devtest/devtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ func main() {
for _, y := range []int{1} {
callThirdPartyAcceptListed(&y)
}
var y UnsafeStruct
for _, x := range []int{1, 2, 3} { // want `reference to x was used in a composite literal at line 34`
y = UnsafeStruct{&x}
}
for _, y := range []int{1} { // want `reference to y was used in a composite literal at line 37`
useUnsafeStruct(UnsafeStruct{&y})
}
var x *int
for _, z := range []int{1} { // want `reference to z is reassigned at line 41`
x = &z
}
fmt.Println(x, y) // for use
}

func useUnsafeStruct(x UnsafeStruct) {
fmt.Println(x)
}

type UnsafeStruct struct {
x *int
}

type A struct {
Expand Down Expand Up @@ -146,5 +166,5 @@ func callThirdPartyAcceptListed1(x *int) {
}

func callThirdPartyAcceptListed2(x *int) {
fmt.Printf(x) // fmt.Printf *is* accept-listed;
fmt.Printf("%v", x) // fmt.Printf *is* accept-listed;
}

0 comments on commit 4f4a1da

Please sign in to comment.