Skip to content

Commit

Permalink
Fail StorableSlab.Encode() with inlined array/map
Browse files Browse the repository at this point in the history
This commit makes StorableSlab.Encode() return
error if it contains inlined array or inlined map.
  • Loading branch information
fxamacker committed Oct 24, 2023
1 parent d88bd12 commit acdb685
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func (a *ArrayDataSlab) Encode(enc *Encoder) error {
h.setRoot()
}

if !elementEnc.inlinedExtraData().empty() {
if elementEnc.hasInlinedExtraData() {
h.setHasInlinedSlabs()
}

Expand All @@ -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().
Expand Down
7 changes: 7 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2718,7 +2718,7 @@ func (m *MapDataSlab) Encode(enc *Encoder) error {
h.setRoot()
}

if !elemEnc.inlinedExtraData().empty() {
if elemEnc.hasInlinedExtraData() {
h.setHasInlinedSlabs()
}

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions storable_slab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit acdb685

Please sign in to comment.