-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathloopcondition.go
47 lines (40 loc) · 909 Bytes
/
loopcondition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package loopcondition
import (
"go/ast"
"go/token"
"go/types"
"github.com/gtramontina/ooze/viruses"
)
type LoopCondition struct {
falseExpression *ast.BinaryExpr
}
// New returns a new LoopCondition virus.
//
// It replaces loop condition with an always false value.
func New() *LoopCondition {
return &LoopCondition{
falseExpression: &ast.BinaryExpr{
X: ast.NewIdent("0"),
OpPos: 0,
Op: token.NEQ,
Y: ast.NewIdent("0"),
},
}
}
func (v *LoopCondition) Incubate(node ast.Node, _ *types.Info) []*viruses.Infection {
statement, matches := node.(*ast.ForStmt)
if !matches {
return nil
}
originalCondition, matches := statement.Cond.(*ast.BinaryExpr)
if !matches {
return nil
}
return []*viruses.Infection{
viruses.NewInfection(
"Loop Condition",
func() { statement.Cond = v.falseExpression },
func() { statement.Cond = originalCondition },
),
}
}