Skip to content

write/read roundtrip fails for certain data combinations #164

Open

Description

@d-lamb

I was working the Synthetic Packets, Fixed Length example and the final statement result = pkt.load("test.bin") failed with RuntimeError: Packet definition larger than packet length by 1 bits (packet length in file is 16552 bits, defined fields are 16553 bits).

Debugging, I found that if I comment out the DATAFLAG lines, the example works fine.
If the DATAFLAG line is given bit_length=8, it works fine.

I reduced it further: if the packet definition only has the DATAFLAG (bit_length=1) type, it works, but if there are any other entries, it does not work. I put that into a test which currently fails, based on the example code, in #165. I have not figured out the source of the problem yet. Based on the error it seems the DATAFLAG PacketField is not getting written to the file, but I have not verified that.

Activity

  1. ddasilva commented on Oct 15, 2026

    @ddasilva
    Collaborator

    Hi @d-lamb, thanks for reporting this !

    I think this is because packets have to be an number of bits divisible by 8 (a clean number of bytes)-- and this error is not handled very well. The fact that it works with just DATAFLAG of bit_length=1 means it's probably adding unprompted padding. The reason it needs to be a clean number of bytes is minimally because the packet length in the header is in a number of bytes.

    Does this make sense to you? I think we should (a) make the example work, possibly by adding a 7-bit fill field after the DATAFLAG so the example runs, and (b) maybe consider adding a better error message when the packet definition is not a clean number of bytes.

  2. ddasilva commented on Oct 20, 2026

    @ddasilva
    Collaborator

    I have changed the synthetic packet documentation page to have bit_length=8 so the examples work. I didn't hear back from you, so I will assume this settles your concern.

    -Daniel

  3. d-lamb commented on Oct 20, 2026

    @d-lamb
    ContributorAuthor

    Sorry for the delay, I was looking at some options. Setting the bit_length=8 in the DATAFLAG field makes the example work for me.

    If you want to raise an error when a packet is defined that doesn't have multiples of 8 bits, you could do this in packet_types.py:

            if description is not None and not isinstance(description, str):
                raise TypeError("description must be a str")
    
            total_bit_length = sum([f.bit_length for f in fields])
            if total_bit_length % BITS_PER_BYTE:
                raise ValueError(f"The sum of bit_length values must be a multiple of {BITS_PER_BYTE}. {total_bit_length} bits defined. Consider using fill bits.")

    But that does cause a failure of test_nbytes_file_too_long where you have a 12-bit packet defined. Depends on how strict you want to be with that 8-bit-multiples rule.

    I reworked the test in the now-closed PR to catch that error and to check the round-trip with fill bits added, and can open a new PR if you want.

  4. ddasilva commented on Oct 20, 2026

    @ddasilva
    Collaborator

    Thanks for putting together a patch for to check this explicitly.

    I think your snippet needs to be adjusted to account for the PacketArray class in how it computes the total bit length. I would recommend looping over fields and checking isinstance(field, PacketArray).

    In test_nbytes_file_too_long we don't have a 12 bit packet defined, we have a 12 * 8 bit packet defined because it's an array of shape (8, 1), (and that is divisible by 8).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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