internal/result: change methods to functions
Change all methods in package result to functions, as a step towards
reducing the package to being only structs with no dependencies.
Change-Id: I0208b82dffbac8824b3e41a893d25563a38db2d5
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/474756
Reviewed-by: Julie Qiu <julieqiu@google.com>
Run-TryBot: Julie Qiu <julieqiu@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/govulncheck/callstacks.go b/internal/govulncheck/callstacks.go
index df861d9..14a7ce2 100644
--- a/internal/govulncheck/callstacks.go
+++ b/internal/govulncheck/callstacks.go
@@ -141,7 +141,7 @@
return ""
}
- topPos := internal.AbsRelShorter(cs.Frames[iTop].Pos())
+ topPos := internal.AbsRelShorter(result.Pos(cs.Frames[iTop]))
if topPos != "" {
topPos += ": "
}
@@ -159,7 +159,7 @@
if iVulnStart != iTopEnd+1 {
// If there is something in between top and vuln segments of
// the stack, then also summarize that intermediate segment.
- return fmt.Sprintf("%s%s calls %s, which eventually calls %s", topPos, topFunc, cs.Frames[iTopEnd+1].Name(), vulnFunc)
+ return fmt.Sprintf("%s%s calls %s, which eventually calls %s", topPos, topFunc, result.FuncName(cs.Frames[iTopEnd+1]), vulnFunc)
}
if vulnStartFunc != vulnFunc {
// The first function of the vuln segment is anonymous.
@@ -185,7 +185,7 @@
return -1, -1, "", ""
}
- topEndFunc = frames[iTopEnd].Name()
+ topEndFunc = result.FuncName(frames[iTopEnd])
if !isAnonymousFunction(topEndFunc) {
iTop = iTopEnd
topFunc = topEndFunc
@@ -203,7 +203,7 @@
return
}
- topFunc = frames[iTop].Name()
+ topFunc = result.FuncName(frames[iTop])
return
}
@@ -225,7 +225,7 @@
}
iVulnStart += iTop + 1 // adjust for slice in call to highest.
- vulnStartFunc = frames[iVulnStart].Name()
+ vulnStartFunc = result.FuncName(frames[iVulnStart])
if !isAnonymousFunction(vulnStartFunc) {
vulnFunc = vulnStartFunc
return
@@ -241,7 +241,7 @@
return
}
- vulnFunc = frames[iVuln+iVulnStart].Name()
+ vulnFunc = result.FuncName(frames[iVuln+iVulnStart])
return
}
diff --git a/internal/govulncheck/result_test.go b/internal/govulncheck/result_test.go
index d0d52c4..302043b 100644
--- a/internal/govulncheck/result_test.go
+++ b/internal/govulncheck/result_test.go
@@ -36,10 +36,10 @@
"",
},
} {
- if got := test.sf.Name(); got != test.wantFunc {
+ if got := result.FuncName(test.sf); got != test.wantFunc {
t.Errorf("want %v func name; got %v", test.wantFunc, got)
}
- if got := test.sf.Pos(); got != test.wantPos {
+ if got := result.Pos(test.sf); got != test.wantPos {
t.Errorf("want %v call position; got %v", test.wantPos, got)
}
}
@@ -79,7 +79,7 @@
{"not called - single module", vuln([2]string{"golang.org/p1", ""}), false},
{"not called - multi modules", vuln([2]string{"golang.org/p1", ""}, [2]string{"golang.org/p2", ""}), false},
} {
- if test.v.IsCalled() != test.want {
+ if result.IsCalled(test.v) != test.want {
t.Errorf("want called=%t for %v; got the opposite", test.want, test.desc)
}
}
diff --git a/internal/govulncheck/text.go b/internal/govulncheck/text.go
index b454987..ebda9df 100644
--- a/internal/govulncheck/text.go
+++ b/internal/govulncheck/text.go
@@ -117,7 +117,7 @@
// TODO(https://go.dev/issue/58945): add a test for this
if source {
for _, v := range r.Vulns {
- if v.IsCalled() {
+ if result.IsCalled(v) {
return ErrVulnerabilitiesFound
}
}
@@ -140,7 +140,7 @@
var unaffected []tmplVulnInfo
var affected []tmplVulnInfo
for _, v := range r.Vulns {
- if !source || v.IsCalled() {
+ if !source || result.IsCalled(v) {
affected = append(affected, createTmplVulnInfo(v, verbose, source))
} else {
// save arbitrary module info for informational message
@@ -276,8 +276,8 @@
for _, cs := range css {
b.WriteString(fmt.Sprintf("#%d: for function %s\n", i, cs.Symbol))
for _, e := range cs.Frames {
- b.WriteString(fmt.Sprintf(" %s\n", e.Name()))
- if pos := internal.AbsRelShorter(e.Pos()); pos != "" {
+ b.WriteString(fmt.Sprintf(" %s\n", result.FuncName(e)))
+ if pos := internal.AbsRelShorter(result.Pos(e)); pos != "" {
b.WriteString(fmt.Sprintf(" %s\n", pos))
}
}
diff --git a/internal/result/result.go b/internal/result/result.go
index 16eceea..83e3b38 100644
--- a/internal/result/result.go
+++ b/internal/result/result.go
@@ -36,7 +36,7 @@
// IsCalled reports whether the vulnerability is called, therefore
// affecting the target source code or binary.
-func (v *Vuln) IsCalled() bool {
+func IsCalled(v *Vuln) bool {
for _, m := range v.Modules {
for _, p := range m.Packages {
if len(p.CallStacks) > 0 {
@@ -133,9 +133,9 @@
Position token.Position
}
-// Name returns the full qualified function name from sf,
+// FuncName returns the full qualified function name from sf,
// adjusted to remove pointer annotations.
-func (sf *StackFrame) Name() string {
+func FuncName(sf *StackFrame) string {
var n string
if sf.RecvType == "" {
n = fmt.Sprintf("%s.%s", sf.PkgPath, sf.FuncName)
@@ -147,7 +147,7 @@
// Pos returns the position of the call in sf as string.
// If position is not available, return "".
-func (sf *StackFrame) Pos() string {
+func Pos(sf *StackFrame) string {
if sf.Position.IsValid() {
return sf.Position.String()
}