Kaynağa Gözat

libnetwork/datastore: prevent accidental recursion

The datastore cache only uses the reference to its datastore to get a
reference to the backing store. Modify the cache to take the backing
store reference directly so that methods on the datastore can't get
called, as that might result in infinite recursion between datastore and
cache methods.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 1 yıl önce
ebeveyn
işleme
5b3086db1f

+ 3 - 3
libnetwork/datastore/cache.go

@@ -13,10 +13,10 @@ type kvMap map[string]KVObject
 type cache struct {
 	sync.Mutex
 	kmm map[string]kvMap
-	ds  *Store
+	ds  store.Store
 }
 
-func newCache(ds *Store) *cache {
+func newCache(ds store.Store) *cache {
 	return &cache{kmm: make(map[string]kvMap), ds: ds}
 }
 
@@ -40,7 +40,7 @@ func (c *cache) kmap(kvObject KVObject) (kvMap, error) {
 		return nil, errors.New("error while populating kmap, object does not implement KVConstructor interface")
 	}
 
-	kvList, err := c.ds.store.List(keyPrefix)
+	kvList, err := c.ds.List(keyPrefix)
 	if err != nil {
 		if err == store.ErrKeyNotFound {
 			// If the store doesn't have anything then there is nothing to

+ 1 - 4
libnetwork/datastore/datastore.go

@@ -143,10 +143,7 @@ func newClient(kv string, addr string, config *store.Config) (*Store, error) {
 		return nil, err
 	}
 
-	ds := &Store{scope: scope.Local, store: s}
-	ds.cache = newCache(ds)
-
-	return ds, nil
+	return &Store{scope: scope.Local, store: s, cache: newCache(s)}, nil
 }
 
 // New creates a new Store instance.