Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions cmd/govulncheck/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"sync"
"testing"
"unsafe"
Expand Down Expand Up @@ -239,10 +240,5 @@ func runTestSuite(t *testing.T, testfilesDir string, vulndbDir string, cfg *conf
}

func isJSONMode(args []string) bool {
for _, arg := range args {
if arg == "json" {
return true
}
}
return false
return slices.Contains(args, "json")
}
8 changes: 2 additions & 6 deletions cmd/govulncheck/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
"testing"
)

Expand Down Expand Up @@ -85,12 +86,7 @@ type config struct {
}

func (c *config) skip() bool {
for _, sg := range c.SkipGOOS {
if runtime.GOOS == sg {
return true
}
}
return false
return slices.Contains(c.SkipGOOS, runtime.GOOS)
}

type fixup struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/buildinfo/additions_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func Vuln() {
e := packagestest.Export(t, packagestest.Modules, []packagestest.Module{
{
Name: "golang.org/entry",
Files: map[string]interface{}{
Files: map[string]any{
"main.go": `
package main

Expand All @@ -140,7 +140,7 @@ func Vuln() {
}},
{
Name: "golang.org/bmod@v0.5.0",
Files: map[string]interface{}{"bvuln/bvuln.go": fmt.Sprintf(vulnLib, tc.gl)},
Files: map[string]any{"bvuln/bvuln.go": fmt.Sprintf(vulnLib, tc.gl)},
},
})
defer e.Cleanup()
Expand Down
15 changes: 3 additions & 12 deletions internal/buildinfo/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ type elfExe struct {
func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
for _, prog := range x.f.Progs {
if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 {
n := prog.Vaddr + prog.Filesz - addr
if n > size {
n = size
}
n := min(prog.Vaddr+prog.Filesz-addr, size)
data := make([]byte, n)
_, err := prog.ReadAt(data, int64(addr-prog.Vaddr))
if err != nil {
Expand Down Expand Up @@ -130,10 +127,7 @@ func (x *peExe) ReadData(addr, size uint64) ([]byte, error) {
addr -= x.imageBase()
for _, sect := range x.f.Sections {
if uint64(sect.VirtualAddress) <= addr && addr <= uint64(sect.VirtualAddress+sect.Size-1) {
n := uint64(sect.VirtualAddress+sect.Size) - addr
if n > size {
n = size
}
n := min(uint64(sect.VirtualAddress+sect.Size)-addr, size)
data := make([]byte, n)
_, err := sect.ReadAt(data, int64(addr-uint64(sect.VirtualAddress)))
if err != nil {
Expand Down Expand Up @@ -186,10 +180,7 @@ func (x *machoExe) ReadData(addr, size uint64) ([]byte, error) {
if seg.Name == "__PAGEZERO" {
continue
}
n := seg.Addr + seg.Filesz - addr
if n > size {
n = size
}
n := min(seg.Addr+seg.Filesz-addr, size)
data := make([]byte, n)
_, err := seg.ReadAt(data, int64(addr-seg.Addr))
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func (c *Client) ByModules(ctx context.Context, reqs []*ModuleRequest) (_ []*Mod
g, gctx := errgroup.WithContext(ctx)
g.SetLimit(10)
for i, req := range reqs {
i, req := i, req
g.Go(func() error {
entries, err := c.byModule(gctx, req, metas[i])
if err != nil {
Expand Down Expand Up @@ -297,7 +296,6 @@ func (c *Client) byIDs(ctx context.Context, ids []string) (_ []*osv.Entry, err e
g, gctx := errgroup.WithContext(ctx)
g.SetLimit(10)
for i, id := range ids {
i, id := i, id
g.Go(func() error {
e, err := c.byID(gctx, id)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/derrors/derrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// Example:
//
// defer derrors.Wrap(&err, "copy(%s, %s)", dst, src)
func Wrap(errp *error, format string, args ...interface{}) {
func Wrap(errp *error, format string, args ...any) {
if *errp != nil {
*errp = fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), *errp)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/gosym/pclntab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestLineFromAline(t *testing.T) {
// source line monotonically
lastline := make(map[string]int)
final := -1
for i := 0; i < 10000; i++ {
for i := range 10000 {
path, line := pkg.lineFromAline(i)
// Check for end of object
if path == "" {
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestLineAline(t *testing.T) {
// object. alineFromLine will always return alines in
// the first file, so track which lines we've seen.
found := make(map[string]int)
for i := 0; i < 1000; i++ {
for i := range 1000 {
path, line := o.lineFromAline(i)
if path == "" {
break
Expand Down
10 changes: 2 additions & 8 deletions internal/gosym/symtab.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ func (s *Sym) PackageName() string {
return ""
}

pathend := strings.LastIndex(name, "/")
if pathend < 0 {
pathend = 0
}
pathend := max(strings.LastIndex(name, "/"), 0)

if i := strings.Index(name[pathend:], "."); i != -1 {
return name[:pathend+i]
Expand All @@ -88,10 +85,7 @@ func (s *Sym) ReceiverName() string {
name := s.nameWithoutInst()
// If we find a slash in name, it should precede any bracketed expression
// that was removed, so pathend will apply correctly to name and s.Name.
pathend := strings.LastIndex(name, "/")
if pathend < 0 {
pathend = 0
}
pathend := max(strings.LastIndex(name, "/"), 0)
// Find the first dot after pathend (or from the beginning, if there was
// no slash in name).
l := strings.Index(name[pathend:], ".")
Expand Down
5 changes: 1 addition & 4 deletions internal/goversion/exe.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ func (x *elfExe) ReadData(addr, size uint64) ([]byte, error) {
// The following line was commented from the original code.
//fmt.Printf("%#x %#x %#x\n", addr, prog.Vaddr, prog.Vaddr+prog.Filesz)
if prog.Vaddr <= addr && addr <= prog.Vaddr+prog.Filesz-1 {
n := prog.Vaddr + prog.Filesz - addr
if n > size {
n = size
}
n := min(prog.Vaddr+prog.Filesz-addr, size)
data := make([]byte, n)
_, err := prog.ReadAt(data, int64(addr-prog.Vaddr))
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions internal/goversion/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ func findCryptoSigs(v *Version, f exe) error {
const maxSigLen = 1 << 10
start, end := f.TextRange()
for addr := start; addr < end; {
size := uint64(1 << 20)
if end-addr < size {
size = end - addr
}
size := min(end-addr, uint64(1<<20))
data, err := f.ReadData(addr, size)
if err != nil {
return fmt.Errorf("reading text: %v", err)
Expand Down Expand Up @@ -208,10 +205,7 @@ func findModuleInfo(v *Version, f exe) error {
const maxModInfo = 128 << 10
start, end := f.RODataRange()
for addr := start; addr < end; {
size := uint64(4 << 20)
if end-addr < size {
size = end - addr
}
size := min(end-addr, uint64(4<<20))
data, err := f.ReadData(addr, size)
if err != nil {
return fmt.Errorf("reading text: %v", err)
Expand Down
1 change: 0 additions & 1 deletion internal/openvex/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ func TestMoreSpecific(t *testing.T) {
frame("m2", "", "")},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
f1 := &govulncheck.Finding{Trace: tc.trace1}
f2 := &govulncheck.Finding{Trace: tc.trace2}
Expand Down
4 changes: 2 additions & 2 deletions internal/openvex/vex.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Document struct {
Author string `json:"author,omitempty"`

// Timestamp defines the time at which the document was issued.
Timestamp time.Time `json:"timestamp,omitempty"`
Timestamp time.Time `json:"timestamp"`

// Version is the document version. For govulncheck's output, this will always be 1.
Version int `json:"version,omitempty"`
Expand All @@ -64,7 +64,7 @@ type Document struct {
// Statement conveys a single status for a single vulnerability for one or more products.
type Statement struct {
// Vulnerability is the vuln being referenced by the statement.
Vulnerability Vulnerability `json:"vulnerability,omitempty"`
Vulnerability Vulnerability `json:"vulnerability"`

// Products are the products associated with the given vulnerability in the statement.
Products []Product `json:"products,omitempty"`
Expand Down
4 changes: 2 additions & 2 deletions internal/osv/osv.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ type Entry struct {
// GO-<YEAR>-<ENTRYID>.
ID string `json:"id"`
// Modified is the time the entry was last modified. Required.
Modified time.Time `json:"modified,omitempty"`
Modified time.Time `json:"modified"`
// Published is the time the entry should be considered to have
// been published.
Published time.Time `json:"published,omitempty"`
Published time.Time `json:"published"`
// Withdrawn is the time the entry should be considered to have
// been withdrawn. If the field is missing, then the entry has
// not been withdrawn.
Expand Down
9 changes: 4 additions & 5 deletions internal/sarif/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"path/filepath"
"slices"
"sort"

"golang.org/x/vuln/internal"
Expand Down Expand Up @@ -292,9 +293,8 @@ func stack(h *handler, f *govulncheck.Finding) Stack {
trace := f.Trace
top := trace[len(trace)-1] // belongs to top level module

frames := make([]Frame, 0, len(trace)) // must not be nil
for i := len(trace) - 1; i >= 0; i-- { // vulnerable symbol is at the top frame
frame := trace[i]
frames := make([]Frame, 0, len(trace)) // must not be nil
for _, frame := range slices.Backward(trace) { // vulnerable symbol is at the top frame
pos := govulncheck.Position{Line: 1, Column: 1}
if frame.Position != nil {
pos = *frame.Position
Expand Down Expand Up @@ -365,10 +365,9 @@ func threadFlows(h *handler, fs []*govulncheck.Finding) []ThreadFlow {
top := trace[len(trace)-1] // belongs to top level module

var tf []ThreadFlowLocation
for i := len(trace) - 1; i >= 0; i-- { // vulnerable symbol is at the top frame
for _, frame := range slices.Backward(trace) { // vulnerable symbol is at the top frame
// TODO: should we, similar to govulncheck text output, only
// mention three elements of the compact trace?
frame := trace[i]
pos := govulncheck.Position{Line: 1, Column: 1}
if frame.Position != nil {
pos = *frame.Position
Expand Down
1 change: 0 additions & 1 deletion internal/sarif/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ func TestMoreSpecific(t *testing.T) {
frame("m2", "", "")},
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
f1 := &govulncheck.Finding{Trace: tc.trace1}
f2 := &govulncheck.Finding{Trace: tc.trace2}
Expand Down
32 changes: 16 additions & 16 deletions internal/sarif/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ type Log struct {
// Run summarizes results of a single invocation of a static analysis tool,
// in this case govulncheck.
type Run struct {
Tool Tool `json:"tool,omitempty"`
Tool Tool `json:"tool"`
// Results contain govulncheck findings. There should be exactly one
// Result per a detected use of an OSV.
Results []Result `json:"results"`
}

// Tool captures information about govulncheck analysis that was run.
type Tool struct {
Driver Driver `json:"driver,omitempty"`
Driver Driver `json:"driver"`
}

// Driver provides details about the govulncheck binary being executed.
Expand All @@ -83,7 +83,7 @@ type Driver struct {
// InformationURI points to the description of govulncheck tool
InformationURI string `json:"informationUri,omitempty"`
// Properties are govulncheck run metadata, such as vuln db, Go version, etc.
Properties govulncheck.Config `json:"properties,omitempty"`
Properties govulncheck.Config `json:"properties"`

Rules []Rule `json:"rules"`
}
Expand All @@ -93,14 +93,14 @@ type Driver struct {
type Rule struct {
// ID is OSV.ID
ID string `json:"id,omitempty"`
ShortDescription Description `json:"shortDescription,omitempty"`
FullDescription Description `json:"fullDescription,omitempty"`
Help Description `json:"help,omitempty"`
ShortDescription Description `json:"shortDescription"`
FullDescription Description `json:"fullDescription"`
Help Description `json:"help"`
HelpURI string `json:"helpUri,omitempty"`
// Properties contain OSV.Aliases (CVEs and GHSAs) as tags.
// Consumers of govulncheck SARIF can use these tags to filter
// results.
Properties RuleTags `json:"properties,omitempty"`
Properties RuleTags `json:"properties"`
}

// RuleTags defines properties.tags.
Expand All @@ -126,7 +126,7 @@ type Result struct {
// Level is one of "error", "warning", and "note".
Level string `json:"level,omitempty"`
// Message explains the overall findings.
Message Description `json:"message,omitempty"`
Message Description `json:"message"`
// Locations to which the findings are associated. Always
// a single location pointing to the first line of the go.mod
// file. The path to the file is "go.mod".
Expand All @@ -144,7 +144,7 @@ type Result struct {
type CodeFlow struct {
// ThreadFlows is effectively a set of related information flows.
ThreadFlows []ThreadFlow `json:"threadFlows"`
Message Description `json:"message,omitempty"`
Message Description `json:"message"`
}

// ThreadFlow encodes an information flow as a sequence of locations.
Expand All @@ -159,12 +159,12 @@ type ThreadFlowLocation struct {
// with, say, the source module analyzed.
Module string `json:"module,omitempty"`
// Location also contains a Message field.
Location Location `json:"location,omitempty"`
Location Location `json:"location"`
}

// Stack is a sequence of frames and can encode a govulncheck call stack.
type Stack struct {
Message Description `json:"message,omitempty"`
Message Description `json:"message"`
Frames []Frame `json:"frames"`
}

Expand All @@ -175,18 +175,18 @@ type Frame struct {
// <version> can be empty when the module version is not known as
// with, say, the source module analyzed.
Module string `json:"module,omitempty"`
Location Location `json:"location,omitempty"`
Location Location `json:"location"`
}

// Location is currently a physical location annotated with a message.
type Location struct {
PhysicalLocation PhysicalLocation `json:"physicalLocation,omitempty"`
Message Description `json:"message,omitempty"`
PhysicalLocation PhysicalLocation `json:"physicalLocation"`
Message Description `json:"message"`
}

type PhysicalLocation struct {
ArtifactLocation ArtifactLocation `json:"artifactLocation,omitempty"`
Region Region `json:"region,omitempty"`
ArtifactLocation ArtifactLocation `json:"artifactLocation"`
Region Region `json:"region"`
}

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/sarif/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func symbol(fr *govulncheck.Frame) string {
if fr.Function == "" {
return ""
}
sym := strings.Split(fr.Function, "$")[0]
sym, _, _ := strings.Cut(fr.Function, "$")
if fr.Receiver != "" {
sym = fr.Receiver + "." + sym
}
Expand Down
Loading
Loading