-
Notifications
You must be signed in to change notification settings - Fork 4
/
cmd_bounds.go
78 lines (65 loc) · 1.6 KB
/
cmd_bounds.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
package main
import (
"context"
"flag"
"fmt"
"os/exec"
"regexp"
)
type boundCheckRunner struct {
flagMod string
asJSON bool
messageRE *regexp.Regexp
}
func (r *boundCheckRunner) ExecCommand(_ context.Context, args []string) error {
fset := flag.NewFlagSet("boundCheck", flag.ContinueOnError)
fset.StringVar(&r.flagMod, "mod", "", `-mod compiler flag(readonly|vendor)`)
fset.BoolVar(&r.asJSON, "json", false, `return result as JSON`)
if err := fset.Parse(args); err != nil {
return err
}
args = fset.Args()
if len(args) == 0 {
args = []string{"."}
}
r.messageRE = regexp.MustCompile(`(.*:\d+:\d+): Found Is(Slice)?InBounds`)
for _, pkg := range args {
if err := r.process(pkg); err != nil {
fmt.Printf("%s: %v", pkg, err)
}
}
return nil
}
func (r *boundCheckRunner) process(pkg string) error {
cmd := exec.Command("go", r.getCmdArgs(pkg)...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%v: %s", err, out)
}
type boundCheckResult struct {
Loc string `json:"loc"`
}
results := []boundCheckResult{}
for _, submatches := range r.messageRE.FindAllStringSubmatch(string(out), -1) {
loc := submatches[1]
results = append(results, boundCheckResult{
Loc: loc,
})
}
if r.asJSON {
marshalJSON(results)
return nil
}
for _, r := range results {
fmt.Printf("%s: slice/array has bound checks\n", r.Loc)
}
return nil
}
func (r *boundCheckRunner) getCmdArgs(pkg string) []string {
args := []string{"build", "-gcflags", "-d=ssa/check_bce/debug=1"}
if r.flagMod != "" {
args = append(args, "-mod", r.flagMod)
}
args = append(args, pkg)
return args
}