Skip to content

Commit 2584928

Browse files
committed
internal/symbols: remove logging from Patched
Bubble up the errors instead of passing a logging function. Change-Id: Id55c3588170ae822b09ede3ed0e0224089be147b Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/560777 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Maceo Thompson <[email protected]> Reviewed-by: Zvonimir Pavlinovic <[email protected]>
1 parent ebe3f50 commit 2584928

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

cmd/vulnreport/symbols.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (s *symbolsCmd) run(ctx context.Context, filename string) (err error) {
5656
for i, fixLink := range defaultFixes {
5757
fixHash := filepath.Base(fixLink)
5858
fixRepo := strings.TrimSuffix(fixLink, "/commit/"+fixHash)
59-
pkgsToSymbols, err := symbols.Patched(mod.Module, fixRepo, fixHash, log.Errf)
59+
pkgsToSymbols, err := symbols.Patched(mod.Module, fixRepo, fixHash)
6060
if err != nil {
6161
log.Err(err)
6262
continue

internal/symbols/patched_functions.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535
// patched in the package. Test packages and symbols are omitted.
3636
//
3737
// If the commit has more than one parent, an error is returned.
38-
func Patched(module, repoURL, commitHash string, errf logf) (_ map[string][]string, err error) {
38+
func Patched(module, repoURL, commitHash string) (_ map[string][]string, err error) {
3939
defer derrors.Wrap(&err, "Patched(%s, %s, %s)", module, repoURL, commitHash)
4040

4141
repoRoot, err := os.MkdirTemp("", commitHash)
@@ -90,7 +90,10 @@ func Patched(module, repoURL, commitHash string, errf logf) (_ map[string][]stri
9090
return nil, err
9191
}
9292

93-
patched := patchedSymbols(oldSymbols, newSymbols, errf)
93+
patched, err := patchedSymbols(oldSymbols, newSymbols)
94+
if err != nil {
95+
return nil, err
96+
}
9497
pkgSyms := make(map[string][]string)
9598
for _, sym := range patched {
9699
pkgSyms[sym.pkg] = append(pkgSyms[sym.pkg], sym.symbol)
@@ -101,7 +104,7 @@ func Patched(module, repoURL, commitHash string, errf logf) (_ map[string][]stri
101104
// patchedSymbols returns symbol indices in oldSymbols that either 1) cannot
102105
// be identified in newSymbols or 2) the corresponding functions have their
103106
// source code changed.
104-
func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl, errf logf) []symKey {
107+
func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl) ([]symKey, error) {
105108
var syms []symKey
106109
for key, of := range oldSymbols {
107110
nf, ok := newSymbols[key]
@@ -112,23 +115,30 @@ func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl, errf logf)
112115
continue
113116
}
114117

115-
if source(of, errf) != source(nf, errf) {
118+
osrc, err := source(of)
119+
if err != nil {
120+
return nil, err
121+
}
122+
nsrc, err := source(nf)
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
if osrc != nsrc {
116128
syms = append(syms, key)
117129
}
118130
}
119-
return syms
131+
return syms, nil
120132
}
121133

122134
// source returns f's source code as text.
123-
func source(f *ast.FuncDecl, errf logf) string {
135+
func source(f *ast.FuncDecl) (string, error) {
124136
var b bytes.Buffer
125137
fs := token.NewFileSet()
126138
if err := printer.Fprint(&b, fs, f); err != nil {
127-
// should not happen, so just printing a warning
128-
errf("getting source of %s failed with %v", astSymbolName(f), err)
129-
return ""
139+
return "", fmt.Errorf("getting source of %s failed: %w", astSymbolName(f), err)
130140
}
131-
return strings.TrimSpace(b.String())
141+
return strings.TrimSpace(b.String()), nil
132142
}
133143

134144
// moduleSymbols indexes all symbols of a module located

internal/symbols/patched_functions_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ func TestPatchedSymbols(t *testing.T) {
4545
if err != nil {
4646
t.Error(err)
4747
}
48-
// logs are not important for this test
49-
discardLog := func(string, ...any) {}
50-
got := toMap(patchedSymbols(oldSyms, newSyms, discardLog))
48+
patched, err := patchedSymbols(oldSyms, newSyms)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
got := toMap(patched)
5153
if diff := cmp.Diff(got, tc.want); diff != "" {
5254
t.Errorf("(-got, want+):\n%s", diff)
5355
}

0 commit comments

Comments
 (0)