From 097ef2a7318b8483e08be510713ac8daf523a7ee Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Sat, 10 Oct 2020 12:37:46 +0300 Subject: [PATCH] core: copy storage item key in simple.GetStorageItems Related to #1468, ported from #1475. We should copy the key to avoid bytes substitution. Otherwise there's a chance that at the end of dao.Store.Seek(...) execution some keys won't be the same as the original keys found inside saveToMap function because storage.Seek can guarantee that provided key and value are only valid until the next `f` call. --- pkg/core/dao/dao.go | 5 ++++- pkg/core/storage/store.go | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/core/dao/dao.go b/pkg/core/dao/dao.go index f4fa5050d..373473320 100644 --- a/pkg/core/dao/dao.go +++ b/pkg/core/dao/dao.go @@ -413,7 +413,10 @@ func (dao *Simple) GetStorageItemsWithPrefix(id int32, prefix []byte) (map[strin } // Cut prefix and hash. - siMap[string(k[len(lookupKey):])] = si + // Must copy here, #1468. + key := make([]byte, len(k[len(lookupKey):])) + copy(key, k[len(lookupKey):]) + siMap[string(key)] = si } dao.Store.Seek(lookupKey, saveToMap) if err != nil { diff --git a/pkg/core/storage/store.go b/pkg/core/storage/store.go index 6c77b425d..ac7ee5f0f 100644 --- a/pkg/core/storage/store.go +++ b/pkg/core/storage/store.go @@ -38,6 +38,8 @@ type ( Get([]byte) ([]byte, error) Put(k, v []byte) error PutBatch(Batch) error + // Seek can guarantee that provided key (k) and value (v) are the only valid until the next call to f. + // Key and value slices should not be modified. Seek(k []byte, f func(k, v []byte)) Close() error }