Skip to content

Commit 48391af

Browse files
committed
cmd/vulnreport,internal/symbols: remove logging from internal/symbols
Return errors instead of logging in internal/symbols functions. Change-Id: I43efd1e27a97a8f6f42abcc361d647fb9eee8f4c Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/562196 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Maceo Thompson <[email protected]> Commit-Queue: Tatiana Bradley <[email protected]> Reviewed-by: Zvonimir Pavlinovic <[email protected]>
1 parent ac87c5f commit 48391af

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

cmd/vulnreport/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func reportFromAliases(ctx context.Context, id, modulePath string, aliases []str
230230
}
231231

232232
if *populateSymbols {
233-
if err := symbols.Populate(r, log.Err); err != nil {
233+
if err := symbols.Populate(r); err != nil {
234234
log.Warnf("could not auto-populate symbols: %s", err)
235235
}
236236
}

cmd/vulnreport/fix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func checkReportSymbols(r *report.Report) error {
137137
log.Infof("%s: skipping symbol checks for package %s (reason: %q)\n", r.ID, p.Package, p.SkipFix)
138138
continue
139139
}
140-
syms, err := symbols.Exported(m, p, log.Errf, log.Err)
140+
syms, err := symbols.Exported(m, p)
141141
if err != nil {
142142
return fmt.Errorf("package %s: %w", p.Package, err)
143143
}

cmd/vulnreport/symbols.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package main
77
import (
88
"context"
99

10-
"golang.org/x/vulndb/cmd/vulnreport/log"
1110
"golang.org/x/vulndb/internal/report"
1211
"golang.org/x/vulndb/internal/symbols"
1312
)
@@ -31,7 +30,7 @@ func (s *symbolsCmd) run(ctx context.Context, filename string) (err error) {
3130
return err
3231
}
3332

34-
if err = symbols.Populate(r, log.Err); err != nil {
33+
if err = symbols.Populate(r); err != nil {
3534
return err
3635
}
3736

internal/symbols/exported_functions.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package symbols
66

77
import (
88
"bytes"
9+
"errors"
910
"fmt"
1011
"go/types"
1112
"os"
@@ -23,7 +24,7 @@ import (
2324

2425
// Exported returns a set of vulnerable symbols, in the vuln
2526
// db format, exported by a package p from the module m.
26-
func Exported(m *report.Module, p *report.Package, errf logf, errln logln) (_ []string, err error) {
27+
func Exported(m *report.Module, p *report.Package) (_ []string, err error) {
2728
defer derrors.Wrap(&err, "Exported(%q, %q)", m.Module, p.Package)
2829

2930
cleanup, err := changeToTempDir()
@@ -36,9 +37,9 @@ func Exported(m *report.Module, p *report.Package, errf logf, errln logln) (_ []
3637
cmd := exec.Command(name, arg...)
3738
out, err := cmd.CombinedOutput()
3839
if err != nil {
39-
errln(string(out))
40+
return fmt.Errorf("%s: %v\nout:\n%s", name, err, string(out))
4041
}
41-
return err
42+
return nil
4243
}
4344

4445
// This procedure was developed through trial and error finding a way
@@ -111,23 +112,8 @@ func Exported(m *report.Module, p *report.Package, errf logf, errln logln) (_ []
111112
// Check to see that all symbols actually exist in the package.
112113
// This should perhaps be a lint check, but lint doesn't
113114
// load/typecheck packages at the moment, so do it here for now.
114-
for _, sym := range p.Symbols {
115-
if typ, method, ok := strings.Cut(sym, "."); ok {
116-
n, ok := pkg.Types.Scope().Lookup(typ).(*types.TypeName)
117-
if !ok {
118-
errf("package %s: %v: type not found\n", p.Package, typ)
119-
continue
120-
}
121-
m, _, _ := types.LookupFieldOrMethod(n.Type(), true, pkg.Types, method)
122-
if m == nil {
123-
errf("package %s: %v: method not found\n", p.Package, sym)
124-
}
125-
} else {
126-
_, ok := pkg.Types.Scope().Lookup(typ).(*types.Func)
127-
if !ok {
128-
errf("package %s: %v: func not found\n", p.Package, typ)
129-
}
130-
}
115+
if err := checkSymbols(pkg, p.Symbols); err != nil {
116+
return nil, fmt.Errorf("invalid symbol(s):\n%w", err)
131117
}
132118

133119
newsyms, err := exportedFunctions(pkg, m)
@@ -153,10 +139,28 @@ func Exported(m *report.Module, p *report.Package, errf logf, errln logln) (_ []
153139
return newslice, nil
154140
}
155141

156-
// TODO(tatianabradley): Refactor functions that use these to return
157-
// info needed to construct logs instead of logging directly.
158-
type logf func(format string, v ...any)
159-
type logln func(v ...any)
142+
func checkSymbols(pkg *packages.Package, symbols []string) error {
143+
var errs []error
144+
for _, sym := range symbols {
145+
if typ, method, ok := strings.Cut(sym, "."); ok {
146+
n, ok := pkg.Types.Scope().Lookup(typ).(*types.TypeName)
147+
if !ok {
148+
errs = append(errs, fmt.Errorf("%v: type not found", typ))
149+
continue
150+
}
151+
m, _, _ := types.LookupFieldOrMethod(n.Type(), true, pkg.Types, method)
152+
if m == nil {
153+
errs = append(errs, fmt.Errorf("%v: method not found", sym))
154+
}
155+
} else {
156+
_, ok := pkg.Types.Scope().Lookup(typ).(*types.Func)
157+
if !ok {
158+
errs = append(errs, fmt.Errorf("%v: func not found", typ))
159+
}
160+
}
161+
}
162+
return errors.Join(errs...)
163+
}
160164

161165
// exportedFunctions returns a set of vulnerable functions exported
162166
// by a packages from the module.

internal/symbols/populate.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package symbols
66

77
import (
8+
"errors"
89
"fmt"
910
"path/filepath"
1011
"strings"
@@ -15,11 +16,11 @@ import (
1516

1617
// Populate attempts to populate the report with symbols derived
1718
// from the patch link(s) in the report.
18-
func Populate(r *report.Report, errln logln) error {
19-
return populate(r, Patched, errln)
19+
func Populate(r *report.Report) error {
20+
return populate(r, Patched)
2021
}
2122

22-
func populate(r *report.Report, patched func(string, string, string) (map[string][]string, error), errln logln) error {
23+
func populate(r *report.Report, patched func(string, string, string) (map[string][]string, error)) error {
2324
var defaultFixes []string
2425

2526
for _, ref := range r.References {
@@ -32,7 +33,7 @@ func populate(r *report.Report, patched func(string, string, string) (map[string
3233
if len(defaultFixes) == 0 {
3334
return fmt.Errorf("no commit fix links found")
3435
}
35-
36+
var errs []error
3637
for _, mod := range r.Modules {
3738
hasFixLink := mod.FixLink != ""
3839
if hasFixLink {
@@ -44,7 +45,7 @@ func populate(r *report.Report, patched func(string, string, string) (map[string
4445
fixRepo := strings.TrimSuffix(fixLink, "/commit/"+fixHash)
4546
pkgsToSymbols, err := patched(mod.Module, fixRepo, fixHash)
4647
if err != nil {
47-
errln(err)
48+
errs = append(errs, err)
4849
continue
4950
}
5051
packages := mod.AllPackages()
@@ -69,7 +70,7 @@ func populate(r *report.Report, patched func(string, string, string) (map[string
6970
}
7071
}
7172

72-
return nil
73+
return errors.Join(errs...)
7374
}
7475

7576
// indexMax takes a slice of nonempty ints and returns the index of the maximum value

internal/symbols/populate_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,7 @@ func TestPopulate(t *testing.T) {
8989
},
9090
} {
9191
t.Run(tc.name, func(t *testing.T) {
92-
discardLog := func(...any) {}
93-
if err := populate(tc.input, patchedFake, discardLog); err != nil {
92+
if err := populate(tc.input, patchedFake); err != nil {
9493
t.Fatal(err)
9594
}
9695
got := tc.input

0 commit comments

Comments
 (0)