Skip to content

Commit

Permalink
Fix SSZ Union not to allow for extra data after the None selector (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeme authored Feb 15, 2025
1 parent 4739b5a commit 55ac17c
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions ssz_serialization/codec.nim
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,21 @@ macro initSszUnionImpl(RecordType: type, input: openArray[byte]): untyped =
raiseMalformedSszError(`type recordDef`, "empty union not allowed")

var selector: `selectorFieldType`
# TODO: `checkedEnumAssign` does not check for holes in an enum.
# SSZ Union spec also doesn't allow this, so it should be fine, but
# nothing stops currently defining an enum with holes and such data
# will also be parsed and result in an object like:
# `(selector: 2 (invalid data!))`
# `checkedEnumAssign` also checks for holes in an enum.
if not checkedEnumAssign(selector, `input`[0]):
raiseMalformedSszError(`type recordDef`, "union selector is out of bounds")

var caseObj = `TInst`(`selectorFieldName`: selector)

var fieldCount = 0
enumInstanceSerializedFields(caseObj, fieldName, field):
when fieldName != `SelectorFieldNameLit`:
readSszValue(`input`.toOpenArray(1, `input`.len - 1), field)
fieldCount.inc()

if fieldCount == 0: # This represents a `None` in the Union
if `input`.len != 1:
raiseMalformedSszError(`type recordDef`, "Union None should have no value")

caseObj

Expand Down

0 comments on commit 55ac17c

Please sign in to comment.