-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsafe.go
80 lines (73 loc) · 1.71 KB
/
safe.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"strconv"
"strings"
)
func (c *Config) isSafe(code string) bool {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "", code, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return false
}
for _, imp := range node.Imports {
importPath := strings.Trim(imp.Path.Value, "\"")
for _, banned := range c.Module {
if strings.HasPrefix(importPath, banned) {
return false
}
}
}
return true
}
func (c *Config) InfiniteLoop(code string) (bool, error) {
fset := token.NewFileSet()
node, err := parser.ParseFile(fset, "", code, parser.AllErrors)
if err != nil {
return true, err
}
var hasInfiniteLoop bool = false
ast.Inspect(node, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.ForStmt:
if x.Cond == nil {
hasInfiniteLoop = true
return false
}
if binExpr, ok := x.Cond.(*ast.BinaryExpr); ok && binExpr.Op == token.LEQ {
if ident, ok := binExpr.Y.(*ast.BasicLit); ok && ident.Kind == token.INT {
if val, err := strconv.Atoi(ident.Value); err == nil && val > c.MaxInt {
hasInfiniteLoop = true
return false
}
}
}
case *ast.RangeStmt:
if x.Key != nil || x.Value != nil {
if lit, ok := x.X.(*ast.BasicLit); ok && lit.Kind == token.INT {
if val, err := strconv.Atoi(lit.Value); err == nil && val > c.MaxInt {
hasInfiniteLoop = true
return false
}
}
}
case *ast.SelectStmt:
hasInfiniteLoop = true
return false
case *ast.SwitchStmt:
hasInfiniteLoop = true
return false
case *ast.BranchStmt:
if x.Tok == token.GOTO {
hasInfiniteLoop = true
return false
}
}
return true
})
return hasInfiniteLoop, nil
}