@@ -10,8 +10,6 @@ import (
10
10
"fmt"
11
11
"os"
12
12
"path/filepath"
13
- "runtime/pprof"
14
-
15
13
"strings"
16
14
17
15
"golang.org/x/tools/go/buildutil"
26
24
jsonFlag = flag .Bool ("json" , false , "output JSON" )
27
25
verboseFlag = flag .Bool ("v" , false , "print a full call stack for each vulnerability" )
28
26
testFlag = flag .Bool ("test" , false , "analyze test files. Only valid for source code." )
29
- cpuprofile = flag .String ("cpuprofile" , "" , "write CPU profile to file" )
30
-
31
- tagsFlag buildutil.TagsFlag
27
+ tagsFlag buildutil.TagsFlag
32
28
33
29
// testmode flags. See main_testmode.go.
34
30
dirFlag string
@@ -62,16 +58,6 @@ For details, see https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck.
62
58
os .Exit (1 )
63
59
}
64
60
65
- // Profiling support.
66
- if * cpuprofile != "" {
67
- f , err := os .Create (* cpuprofile )
68
- if err != nil {
69
- fmt .Fprintln (os .Stderr , err )
70
- os .Exit (1 )
71
- }
72
- pprof .StartCPUProfile (f )
73
- }
74
-
75
61
patterns := flag .Args ()
76
62
77
63
sourceAnalysis := true
@@ -80,18 +66,14 @@ For details, see https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck.
80
66
}
81
67
validateFlags (sourceAnalysis )
82
68
83
- err := doGovulncheck (patterns , sourceAnalysis )
84
- pprof .StopCPUProfile ()
85
- if err != nil {
86
- if code , ok := err .(exitCode ); ok {
87
- os .Exit (int (code ))
88
- }
89
- die ("govulncheck: %v" , err )
69
+ if err := doGovulncheck (patterns , sourceAnalysis ); err != nil {
70
+ die (fmt .Sprintf ("govulncheck: %v" , err ))
90
71
}
91
72
}
92
73
93
- // doGovulncheck performs the main govulncheck functionality and
94
- // returns an error, possibly an exitCode.
74
+ // doGovulncheck performs main govulncheck functionality and exits the
75
+ // program upon success with an appropriate exit status. Otherwise,
76
+ // returns an error.
95
77
func doGovulncheck (patterns []string , sourceAnalysis bool ) error {
96
78
ctx := context .Background ()
97
79
dir := filepath .FromSlash (dirFlag )
@@ -145,11 +127,11 @@ func doGovulncheck(patterns []string, sourceAnalysis bool) error {
145
127
146
128
if * jsonFlag {
147
129
// Following golang.org/x/tools/go/analysis/singlechecker,
148
- // -json mode is always a success .
130
+ // return 0 exit code in -json mode .
149
131
if err := printJSON (res ); err != nil {
150
132
return err
151
133
}
152
- return nil // success
134
+ os . Exit ( 0 )
153
135
}
154
136
155
137
printText (res , * verboseFlag , sourceAnalysis )
@@ -162,21 +144,16 @@ func doGovulncheck(patterns []string, sourceAnalysis bool) error {
162
144
if sourceAnalysis {
163
145
for _ , v := range res .Vulns {
164
146
if v .IsCalled () {
165
- return exitCode (3 )
147
+ os . Exit (3 )
166
148
}
167
149
}
168
150
} else if len (res .Vulns ) > 0 {
169
- return exitCode (3 )
151
+ os . Exit (3 )
170
152
}
153
+ os .Exit (0 )
171
154
return nil
172
155
}
173
156
174
- // exitCode is an error returned by doGovulncheck to indicate
175
- // that the the program should silently exit with the specified code.
176
- type exitCode int
177
-
178
- func (code exitCode ) Error () string { return fmt .Sprintf ("exit code %d" , code ) }
179
-
180
157
func validateFlags (source bool ) {
181
158
if ! source {
182
159
if * testFlag {
0 commit comments