From acdb685d6dcd585f6e697cae1fb3f30623b63e54 Mon Sep 17 00:00:00 2001 From: Faye Amacker <33205765+fxamacker@users.noreply.github.com> Date: Tue, 24 Oct 2023 15:19:11 -0500 Subject: [PATCH] Fail StorableSlab.Encode() with inlined array/map This commit makes StorableSlab.Encode() return error if it contains inlined array or inlined map. --- array.go | 4 ++-- encode.go | 7 +++++++ map.go | 4 ++-- storable_slab.go | 4 ++++ 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/array.go b/array.go index 715d639d..cf3217c8 100644 --- a/array.go +++ b/array.go @@ -828,7 +828,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error { h.setRoot() } - if !elementEnc.inlinedExtraData().empty() { + if elementEnc.hasInlinedExtraData() { h.setHasInlinedSlabs() } @@ -848,7 +848,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error { } // Encode inlined extra data - if !elementEnc.inlinedExtraData().empty() { + if elementEnc.hasInlinedExtraData() { err = elementEnc.inlinedExtraData().Encode(enc) if err != nil { // err is already categorized by inlinedExtraData.Encode(). diff --git a/encode.go b/encode.go index 00b7e08f..d82d56aa 100644 --- a/encode.go +++ b/encode.go @@ -50,6 +50,13 @@ func (enc *Encoder) inlinedExtraData() *InlinedExtraData { return enc._inlinedExtraData } +func (enc *Encoder) hasInlinedExtraData() bool { + if enc._inlinedExtraData == nil { + return false + } + return !enc._inlinedExtraData.empty() +} + type StorableDecoder func( decoder *cbor.StreamDecoder, storableSlabID SlabID, diff --git a/map.go b/map.go index fff1c671..63db77ab 100644 --- a/map.go +++ b/map.go @@ -2718,7 +2718,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error { h.setRoot() } - if !elemEnc.inlinedExtraData().empty() { + if elemEnc.hasInlinedExtraData() { h.setHasInlinedSlabs() } @@ -2738,7 +2738,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error { } // Encode inlined types - if !elemEnc.inlinedExtraData().empty() { + if elemEnc.hasInlinedExtraData() { err = elemEnc.inlinedExtraData().Encode(enc) if err != nil { return NewEncodingError(err) diff --git a/storable_slab.go b/storable_slab.go index 9cc6d7bd..07db18f4 100644 --- a/storable_slab.go +++ b/storable_slab.go @@ -93,6 +93,10 @@ func (s *StorableSlab) Encode(enc *Encoder) error { return wrapErrorfAsExternalErrorIfNeeded(err, "failed to encode storable") } + if enc.hasInlinedExtraData() { + return NewEncodingError(fmt.Errorf("failed to encode storable slab because storable contains inlined array/map")) + } + return nil }