Skip to content

Commit a518b79

Browse files
committed
gopls/internal/vulncheck: synchronize cache access
Corresponds to https://go.dev/cl/396594 adds basic support for cache thread safety https://go.dev/cl/398115 change references to GCS bucket to vuln.go.dev Change-Id: I16587a703431e1e28bc6d5f84ab54b4c88fcbdce Reviewed-on: https://go-review.googlesource.com/c/tools/+/405794 Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent ad497c6 commit a518b79

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

gopls/internal/vulncheck/cache.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io/ioutil"
1111
"os"
1212
"path/filepath"
13+
"sync"
1314
"time"
1415

1516
"golang.org/x/vuln/client"
@@ -18,12 +19,6 @@ import (
1819

1920
// copy from x/vuln/cmd/govulncheck/cache.go
2021

21-
// NOTE: this cache implementation should be kept internal to the go tooling
22-
// (i.e. cmd/go/internal/something) so that the vulndb cache is owned by the
23-
// go command. Also it is currently NOT CONCURRENCY SAFE since it does not
24-
// implement file locking. If ported to the stdlib it should use
25-
// cmd/go/internal/lockedfile.
26-
2722
// The cache uses a single JSON index file for each vulnerability database
2823
// which contains the map from packages to the time the last
2924
// vulnerability for that package was added/modified and the time that
@@ -42,9 +37,11 @@ import (
4237
// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/{import path}/vulns.json
4338
// []*osv.Entry
4439

45-
// fsCache is file-system cache implementing osv.Cache
46-
// TODO: make cache thread-safe
40+
// fsCache is a thread-safe file-system cache implementing osv.Cache
41+
//
42+
// TODO: use something like cmd/go/internal/lockedfile for thread safety?
4743
type fsCache struct {
44+
mu sync.Mutex
4845
rootDir string
4946
}
5047

@@ -61,6 +58,9 @@ type cachedIndex struct {
6158
}
6259

6360
func (c *fsCache) ReadIndex(dbName string) (client.DBIndex, time.Time, error) {
61+
c.mu.Lock()
62+
defer c.mu.Unlock()
63+
6464
b, err := ioutil.ReadFile(filepath.Join(c.rootDir, dbName, "index.json"))
6565
if err != nil {
6666
if os.IsNotExist(err) {
@@ -76,6 +76,9 @@ func (c *fsCache) ReadIndex(dbName string) (client.DBIndex, time.Time, error) {
7676
}
7777

7878
func (c *fsCache) WriteIndex(dbName string, index client.DBIndex, retrieved time.Time) error {
79+
c.mu.Lock()
80+
defer c.mu.Unlock()
81+
7982
path := filepath.Join(c.rootDir, dbName)
8083
if err := os.MkdirAll(path, 0755); err != nil {
8184
return err
@@ -94,6 +97,9 @@ func (c *fsCache) WriteIndex(dbName string, index client.DBIndex, retrieved time
9497
}
9598

9699
func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
100+
c.mu.Lock()
101+
defer c.mu.Unlock()
102+
97103
b, err := ioutil.ReadFile(filepath.Join(c.rootDir, dbName, p, "vulns.json"))
98104
if err != nil {
99105
if os.IsNotExist(err) {
@@ -109,6 +115,9 @@ func (c *fsCache) ReadEntries(dbName string, p string) ([]*osv.Entry, error) {
109115
}
110116

111117
func (c *fsCache) WriteEntries(dbName string, p string, entries []*osv.Entry) error {
118+
c.mu.Lock()
119+
defer c.mu.Unlock()
120+
112121
path := filepath.Join(c.rootDir, dbName, p)
113122
if err := os.MkdirAll(path, 0777); err != nil {
114123
return err

gopls/internal/vulncheck/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func findGOVULNDB(cfg *packages.Config) []string {
5353
if GOVULNDB := os.Getenv("GOVULNDB"); GOVULNDB != "" {
5454
return strings.Split(GOVULNDB, ",")
5555
}
56-
return []string{"https://storage.googleapis.com/go-vulndb"}
56+
return []string{"https://vuln.go.dev"}
5757
}
5858

5959
type Vuln = command.Vuln

0 commit comments

Comments
 (0)