From b030c331b03237a0d245359a1404f769a17ba5f6 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 5 Mar 2024 13:40:13 +0300 Subject: [PATCH] core: copy SC storage iterator prefix while returning values It's a bug since Prefix is shared between all iterator items and appending is not enough. If prefix has enough capacity, then new slice won't be created and the previous item's prefix will be changed. This commit fixes the following test failure caused by moving from bytes.Clone to slice.Copy: ``` --- FAIL: TestComlileAndInvokeFunction/test_Storage.Find (0.02s) --- FAIL: TestComlileAndInvokeFunction/test_Storage.Find/keys_only (0.01s) contract_test.go:866: Error Trace: /home/anna/Documents/GitProjects/nspcc-dev/neo-go/cli/smartcontract/contract_test.go:866 Error: Not equal: expected: []stackitem.Item{(*stackitem.ByteArray)(0xc000a1cdf8), (*stackitem.ByteArray)(0xc000a1ce10)} actual : []stackitem.Item{(*stackitem.ByteArray)(0xc000a1cdb0), (*stackitem.ByteArray)(0xc000a1cdc8)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ (*stackitem.ByteArray)((len=8) { - 00000000 66 69 6e 64 6b 65 79 31 |findkey1| + 00000000 66 69 6e 64 6b 65 79 32 |findkey2| }), Test: TestComlileAndInvokeFunction/test_Storage.Find/keys_only --- FAIL: TestComlileAndInvokeFunction/test_Storage.Find/both (0.01s) contract_test.go:881: Error Trace: /home/anna/Documents/GitProjects/nspcc-dev/neo-go/cli/smartcontract/contract_test.go:881 Error: Not equal: expected: []stackitem.Item{(*stackitem.ByteArray)(0xc000515920), (*stackitem.ByteArray)(0xc000515938)} actual : []stackitem.Item{(*stackitem.ByteArray)(0xc000515848), (*stackitem.ByteArray)(0xc000515860)} Diff: --- Expected +++ Actual @@ -2,3 +2,3 @@ (*stackitem.ByteArray)((len=8) { - 00000000 66 69 6e 64 6b 65 79 31 |findkey1| + 00000000 66 69 6e 64 6b 65 79 32 |findkey2| }), Test: TestComlileAndInvokeFunction/test_Storage.Find/both ``` Signed-off-by: Anna Shaleva --- pkg/core/interop/storage/find.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/core/interop/storage/find.go b/pkg/core/interop/storage/find.go index 666950e34..8a3cc3ba5 100644 --- a/pkg/core/interop/storage/find.go +++ b/pkg/core/interop/storage/find.go @@ -31,6 +31,9 @@ type Iterator struct { curr storage.KeyValue next bool opts int64 + // prefix is the storage item key prefix Find is performed with. It must be + // copied if no FindRemovePrefix option specified since it's shared between all + // iterator items. prefix []byte } @@ -58,7 +61,7 @@ func (s *Iterator) Value() stackitem.Item { } key := s.curr.Key if s.opts&FindRemovePrefix == 0 { - key = append(s.prefix, key...) + key = append(slice.Copy(s.prefix), key...) } if s.opts&FindKeysOnly != 0 { return stackitem.NewByteArray(key)