@@ -3,6 +3,7 @@ package service
33import (
44 "path/filepath"
55 "strings"
6+ "sync"
67
78 "github.com/go-logr/logr"
89 "github.com/konveyor/analyzer-lsp/output/v1/konveyor"
@@ -27,26 +28,32 @@ func NewIncidentsCache(logger logr.Logger) IncidentsCache {
2728 return & incidentsCache {
2829 cache : map [string ][]CacheValue {},
2930 logger : logger ,
31+ mutex : sync.RWMutex {},
3032 }
3133}
3234
3335type incidentsCache struct {
3436 cache map [string ][]CacheValue
3537 logger logr.Logger
38+ mutex sync.RWMutex
3639}
3740
3841func (i * incidentsCache ) Len () int {
3942 return len (i .cache )
4043}
4144
4245func (i * incidentsCache ) Get (path string ) ([]CacheValue , bool ) {
46+ i .mutex .RLock ()
47+ defer i .mutex .RUnlock ()
4348 normalizedPath := normalizePath (path )
4449 i .logger .V (8 ).Info ("getting cache entry for path" , "path" , path , "normalizedPath" , normalizedPath )
4550 val , ok := i .cache [normalizedPath ]
4651 return val , ok
4752}
4853
4954func (i * incidentsCache ) Add (path string , value CacheValue ) {
55+ i .mutex .Lock ()
56+ defer i .mutex .Unlock ()
5057 normalizedPath := normalizePath (path )
5158 i .logger .V (8 ).Info ("adding cache entry for path" , "path" , path , "normalizedPath" , normalizedPath )
5259 if _ , ok := i .cache [normalizedPath ]; ! ok {
@@ -56,12 +63,16 @@ func (i *incidentsCache) Add(path string, value CacheValue) {
5663}
5764
5865func (i * incidentsCache ) Delete (path string ) {
66+ i .mutex .Lock ()
67+ defer i .mutex .Unlock ()
5968 normalizedPath := normalizePath (path )
6069 i .logger .V (8 ).Info ("deleting cache entry for path" , "path" , path , "normalizedPath" , normalizedPath )
6170 delete (i .cache , normalizedPath )
6271}
6372
6473func (i * incidentsCache ) Keys () []string {
74+ i .mutex .RLock ()
75+ defer i .mutex .RUnlock ()
6576 keys := make ([]string , 0 , len (i .cache ))
6677 for k := range i .cache {
6778 keys = append (keys , k )
@@ -70,13 +81,22 @@ func (i *incidentsCache) Keys() []string {
7081}
7182
7283func (i * incidentsCache ) Entries () map [string ][]CacheValue {
73- return i .cache
84+ i .mutex .RLock ()
85+ defer i .mutex .RUnlock ()
86+ // make sure we never return a reference to original map or any of its slices
87+ clone := make (map [string ][]CacheValue , len (i .cache ))
88+ for k , v := range i .cache {
89+ clonedV := make ([]CacheValue , len (v ))
90+ copy (clonedV , v )
91+ clone [k ] = clonedV
92+ }
93+ return clone
7494}
7595
7696func normalizePath (path string ) string {
7797 cleanedPath := filepath .Clean (path )
7898 volumeName := filepath .VolumeName (cleanedPath )
79- // make sure all volume names are lowercase
99+ // make sure all volume names are uppercase
80100 if volumeName != "" {
81101 cleanedPath = strings .ToUpper (volumeName ) + cleanedPath [len (volumeName ):]
82102 }
0 commit comments