Skip to content

Commit

Permalink
fix: add missing nil check in store.GetStore (cosmos#9354) (cosmos#9359)
Browse files Browse the repository at this point in the history
* fix: add missing nil check in store.GetStore

* check nil in rootmulti as well

(cherry picked from commit b065e20)

Co-authored-by: Robert Zaremba <robert@zaremba.ch>
  • Loading branch information
mergify[bot] and robert-zaremba committed May 19, 2021
1 parent 9fe61a7 commit 842b060
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 6 additions & 2 deletions store/cachemulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,17 @@ func (cms Store) CacheMultiStoreWithVersion(_ int64) (types.CacheMultiStore, err

// GetStore returns an underlying Store by key.
func (cms Store) GetStore(key types.StoreKey) types.Store {
return cms.stores[key].(types.Store)
s := cms.stores[key]
if key == nil || s == nil {
panic(fmt.Sprintf("kv store with key %v has not been registered in stores", key))
}
return s.(types.Store)
}

// GetKVStore returns an underlying KVStore by key.
func (cms Store) GetKVStore(key types.StoreKey) types.KVStore {
store := cms.stores[key]
if key == nil {
if key == nil || store == nil {
panic(fmt.Sprintf("kv store with key %v has not been registered in stores", key))
}
return store.(types.KVStore)
Expand Down
23 changes: 23 additions & 0 deletions store/cachemulti/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cachemulti

import (
"fmt"
"testing"

"github.com/cosmos/cosmos-sdk/store/types"
"github.com/stretchr/testify/require"
)

func TestStoreGetKVStore(t *testing.T) {
require := require.New(t)

s := Store{stores: map[types.StoreKey]types.CacheWrap{}}
key := types.NewKVStoreKey("abc")
errMsg := fmt.Sprintf("kv store with key %v has not been registered in stores", key)

require.PanicsWithValue(errMsg,
func() { s.GetStore(key) })

require.PanicsWithValue(errMsg,
func() { s.GetKVStore(key) })
}
6 changes: 5 additions & 1 deletion store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,11 @@ func (rs *Store) GetStore(key types.StoreKey) types.Store {
// NOTE: The returned KVStore may be wrapped in an inter-block cache if it is
// set on the root store.
func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore {
store := rs.stores[key].(types.KVStore)
s := rs.stores[key]
if s == nil {
panic(fmt.Sprintf("store does not exist for key: %s", key.Name()))
}
store := s.(types.KVStore)

if rs.TracingEnabled() {
store = tracekv.NewStore(store, rs.traceWriter, rs.traceContext)
Expand Down

0 comments on commit 842b060

Please sign in to comment.