Skip to content

_cbor2.CBORDecodeEOF: premature end of stream #478

@gg0074x

Description

@gg0074x

Describe the bug
When using lists on a Datum and signing the transaction i noticed that the program failed because of a decoding issue with lists of certain length

To Reproduce
My datum class was:

@dataclass
class Datum(PlutusData):
    CONSTR_ID = 1
    data: list[bytes]

And the list contained the bytes for the string "Hello world!" repeated more than 24 times
Signing a transaction with an output with such Datum fails

Logs

Traceback (most recent call last):
  File "/home/arch/send_datum.py", line 33, in <module>
    datum = LargeDatum.from_cbor(utxo.output.datum.cbor)  # ty:ignore[unresolved-attribute]
  File "/home/arch/.venv/lib/python3.14/site-packages/pycardano/serialization.py", line 543, in from_cbor
    value = cbor2.loads(payload)
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 846, in loads
    ).decode()
      ~~~~~~^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 239, in decode
    return self._decode()
           ~~~~~~~~~~~~^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 226, in _decode
    return decoder(self, subtype)
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 473, in decode_semantic
    tag.value = self._decode(unshared=True)
                ~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 226, in _decode
    return decoder(self, subtype)
  File "/home/arch/.venv/lib/python3.14/site-packages/pycardano/serialization.py", line 203, in decode_array
    ret = IndefiniteFrozenList(list(self.decode_array(subtype=subtype)))
                                    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 411, in decode_array
    value = self._decode()
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 226, in _decode
    return decoder(self, subtype)
  File "/home/arch/.venv/lib/python3.14/site-packages/pycardano/serialization.py", line 207, in decode_array
    return self.decode_array(subtype=subtype)
           ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 425, in decode_array
    items.append(self._decode())
                 ~~~~~~~~~~~~^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 226, in _decode
    return decoder(self, subtype)
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 377, in decode_string
    result = self.read(length).decode("utf-8", self._str_errors)
             ~~~~~~~~~^^^^^^^^
  File "/home/arch/.venv/lib/python3.14/site-packages/cbor2pure/_decoder.py", line 204, in read
    raise CBORDecodeEOF(
    ...<2 lines>...
    )
_cbor2.CBORDecodeEOF: premature end of stream (expected to read 12 bytes, got 3 instead)

It appears that serialization.py consumes a byte that is later needed, the problematic cbor had the bytes:

D8 7A                                # tag(122)
   9F                                # array(*)
      98 18                          # array(24)

where the byte 18 was read by serialization.py and then a call to decode_array read an incorrect length and the content was unaligned from there

Environment and software version:

  • OS: Linux 6.6.87.2-microsoft-standard-WSL2
  • PyCardano Version 0.19.1

Additional context
This bug only happens with cbor2pure and not with the cbor c extension

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions