Skip to content

Commit

Permalink
internal/symbols: add errlog to Patched
Browse files Browse the repository at this point in the history
This makes it consistent with Exported and is a better solution than
simply using fmt.Printf.

Change-Id: I6eeef3034ac7101e6491e81e4b78bd2ecdc5545a
Reviewed-on: https://go-review.googlesource.com/c/vulndb/+/538015
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tatiana Bradley <tatianabradley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
zpavlinovic committed Oct 26, 2023
1 parent f1d9c6a commit 0590c76
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 7 additions & 6 deletions internal/symbols/patched_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"go/printer"
"go/token"
"io/fs"
"log"
"os"
"path"
"path/filepath"
Expand All @@ -35,7 +36,7 @@ import (
// patched in the package. Test packages and symbols are omitted.
//
// If the commit has more than one parent, an error is returned.
func Patched(module, repoURL, commitHash string) (_ map[string][]string, err error) {
func Patched(module, repoURL, commitHash string, errlog *log.Logger) (_ map[string][]string, err error) {
defer derrors.Wrap(&err, "Patched(%s ,%s, %s)", module, repoURL, commitHash)

repoRoot, err := os.MkdirTemp("", commitHash)
Expand Down Expand Up @@ -90,7 +91,7 @@ func Patched(module, repoURL, commitHash string) (_ map[string][]string, err err
return nil, err
}

patched := patchedSymbols(oldSymbols, newSymbols)
patched := patchedSymbols(oldSymbols, newSymbols, errlog)
pkgSyms := make(map[string][]string)
for _, sym := range patched {
pkgSyms[sym.pkg] = append(pkgSyms[sym.pkg], sym.symbol)
Expand All @@ -101,7 +102,7 @@ func Patched(module, repoURL, commitHash string) (_ map[string][]string, err err
// patchedSymbols returns symbol indices in oldSymbols that either 1) cannot
// be identified in newSymbols or 2) the corresponding functions have their
// source code changed.
func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl) []symKey {
func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl, errlog *log.Logger) []symKey {
var syms []symKey
for key, of := range oldSymbols {
nf, ok := newSymbols[key]
Expand All @@ -112,20 +113,20 @@ func patchedSymbols(oldSymbols, newSymbols map[symKey]*ast.FuncDecl) []symKey {
continue
}

if source(of) != source(nf) {
if source(of, errlog) != source(nf, errlog) {
syms = append(syms, key)
}
}
return syms
}

// source returns f's source code as text.
func source(f *ast.FuncDecl) string {
func source(f *ast.FuncDecl, errlog *log.Logger) string {
var b bytes.Buffer
fs := token.NewFileSet()
if err := printer.Fprint(&b, fs, f); err != nil {
// should not happen, so just printing a warning
fmt.Printf("warning: getting source of %s failed with %v", astSymbolName(f), err)
errlog.Printf("getting source of %s failed with %v", astSymbolName(f), err)
return ""
}
return strings.TrimSpace(b.String())
Expand Down
4 changes: 3 additions & 1 deletion internal/symbols/patched_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"go/ast"
"go/parser"
"go/token"
"log"
"os"
"path/filepath"
"sort"
"testing"
Expand Down Expand Up @@ -46,7 +48,7 @@ func TestPatchedSymbols(t *testing.T) {
t.Error(err)
}

got := toMap(patchedSymbols(oldSyms, newSyms))
got := toMap(patchedSymbols(oldSyms, newSyms, log.New(os.Stderr, "ERROR: ", 0)))
if diff := cmp.Diff(got, tc.want); diff != "" {
t.Errorf("(-got, want+):\n%s", diff)
}
Expand Down

0 comments on commit 0590c76

Please sign in to comment.