@@ -91,8 +91,11 @@ func main() {
91
91
ctx := context .Background ()
92
92
93
93
patterns := flag .Args ()
94
- var r * vulncheck.Result
95
- var pkgs []* packages.Package
94
+ var (
95
+ r * vulncheck.Result
96
+ pkgs []* packages.Package
97
+ moduleVersions map [string ]string
98
+ )
96
99
if len (patterns ) == 1 && isFile (patterns [0 ]) {
97
100
f , err := os .Open (patterns [0 ])
98
101
if err != nil {
@@ -113,6 +116,17 @@ func main() {
113
116
if err != nil {
114
117
die ("govulncheck: %v" , err )
115
118
}
119
+ // Build a map from module paths to versions.
120
+ moduleVersions = map [string ]string {}
121
+ packages .Visit (pkgs , nil , func (p * packages.Package ) {
122
+ if m := packageModule (p ); m != nil {
123
+ moduleVersions [m .Path ] = m .Version
124
+ }
125
+ })
126
+
127
+ if len (moduleVersions ) == 0 {
128
+ die ("govulncheck: no modules found; are you in GOPATH mode? Module mode required." )
129
+ }
116
130
r , err = vulncheck .Source (ctx , vulncheck .Convert (pkgs ), vcfg )
117
131
if err != nil {
118
132
die ("govulncheck: %v" , err )
@@ -121,7 +135,7 @@ func main() {
121
135
if * jsonFlag {
122
136
writeJSON (r )
123
137
} else {
124
- writeText (r , pkgs )
138
+ writeText (r , pkgs , moduleVersions )
125
139
}
126
140
exitCode := 0
127
141
// Following go vet, fail with 3 if there are findings (in this case, vulns).
@@ -140,17 +154,10 @@ func writeJSON(r *vulncheck.Result) {
140
154
fmt .Println ()
141
155
}
142
156
143
- func writeText (r * vulncheck.Result , pkgs []* packages.Package ) {
157
+ func writeText (r * vulncheck.Result , pkgs []* packages.Package , moduleVersions map [ string ] string ) {
144
158
if len (r .Vulns ) == 0 {
145
159
return
146
160
}
147
- // Build a map from module paths to versions.
148
- moduleVersions := map [string ]string {}
149
- packages .Visit (pkgs , nil , func (p * packages.Package ) {
150
- if m := packageModule (p ); m != nil {
151
- moduleVersions [m .Path ] = m .Version
152
- }
153
- })
154
161
callStacks := vulncheck .CallStacks (r )
155
162
156
163
const labelWidth = 16
@@ -286,7 +293,7 @@ func representativeFuncs(vg []*vulncheck.Vuln, topPkgs map[string]bool, callStac
286
293
for _ , cs := range callStacks [v ] {
287
294
// Find the lowest function in the stack that is in
288
295
// one of the top packages.
289
- for i := len (cs ) - 1 ; i > 0 ; i -- {
296
+ for i := len (cs ) - 1 ; i >= 0 ; i -- {
290
297
pkg := pkgPath (cs [i ].Function )
291
298
if topPkgs [pkg ] {
292
299
fns [cs [i ].Function ] = true
0 commit comments