From 6879cbcdcc6905533d74bf6e29363aba187aa221 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Tue, 6 Jul 2021 17:10:31 +0300 Subject: [PATCH] stackitem: properly detect recursive references during serialization If we're done with element it no longer can lead to recursion error, so fix cases like `[arr, arr]` where we have two copies of `arr` trigger this error for no good reason (there is no recursion there). --- pkg/vm/stackitem/json.go | 2 ++ pkg/vm/stackitem/serialization.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/pkg/vm/stackitem/json.go b/pkg/vm/stackitem/json.go index ae934ba0e..292b6936a 100644 --- a/pkg/vm/stackitem/json.go +++ b/pkg/vm/stackitem/json.go @@ -241,6 +241,7 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) { arr = append(arr, s) } value = arr + delete(seen, item) case *Bool: value = it.value case *Buffer, *ByteArray: @@ -266,6 +267,7 @@ func toJSONWithTypes(item Item, seen map[Item]bool) (interface{}, error) { }) } value = arr + delete(seen, item) case *Pointer: value = it.pos } diff --git a/pkg/vm/stackitem/serialization.go b/pkg/vm/stackitem/serialization.go index b30cb02c1..bb802dd36 100644 --- a/pkg/vm/stackitem/serialization.go +++ b/pkg/vm/stackitem/serialization.go @@ -82,6 +82,7 @@ func serializeItemTo(item Item, w *io.BinWriter, allowInvalid bool, seen map[Ite for i := range arr { serializeItemTo(arr[i], w, allowInvalid, seen) } + delete(seen, item) case *Map: seen[item] = true @@ -91,6 +92,7 @@ func serializeItemTo(item Item, w *io.BinWriter, allowInvalid bool, seen map[Ite serializeItemTo(t.Value().([]MapElement)[i].Key, w, allowInvalid, seen) serializeItemTo(t.Value().([]MapElement)[i].Value, w, allowInvalid, seen) } + delete(seen, item) case Null: w.WriteB(byte(AnyT)) }