lasp / lasp/space_packet_parser
CCSDSPacketBytes accepts inputs shorter than a primary header, then header properties raise bare IndexError
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 39
- Forks
- 15
- Avg merge
- 2d 16h
- Merged PRs (30d)
- 12
Description
Context
CCSDSPacketBytes (in space_packet_parser/generators/ccsds.py) is a bytes subclass with properties that read the six-byte CCSDS primary header (version_number, type, secondary_header_flag, apid, sequence_flags, sequence_count, data_length, header_values). Nothing checks the length at construction, so an instance shorter than HEADER_LENGTH_BYTES is created without complaint and every header property then fails with an uninformative error:
>>> from space_packet_parser.generators.ccsds import CCSDSPacketBytes
>>> CCSDSPacketBytes(b"\x07").apid
IndexError: index out of range
UDPPacketBytes in generators/udp.py handles the same situation differently. Its __new__ rejects anything shorter than its 8-byte header with ValueError("UDP packet must be at least 8 bytes (got 1 bytes)"). The two bytes subclasses now disagree on how to treat a short input.
#282 made CCSDSPacketBytes.__str__ safe for short input so it can be used in diagnostic messages, but deliberately left construction and the header properties alone because changing them is a compatibility question rather than an error-path fix.
Driving Requirements
A caller who constructs a CCSDSPacketBytes that cannot hold a primary header should get a clear error naming the problem, at a predictable point, consistent with UDPPacketBytes.
Recommended Approach
Validate at construction, matching UDPPacketBytes:
- Add a
__new__toCCSDSPacketBytesthat raisesValueErrorwhenlen(value) < HEADER_LENGTH_BYTES, with a message stating the minimum and actual lengths. - Document the
ValueErrorin the class docstring. - Confirm
ccsds_generatornever constructs a shortCCSDSPacketByteson its truncated-final-packet path, or adjust it so the existing "not enough bytes" handling still produces the current warning rather than a newValueError. - Add unit tests in
tests/unit/test_generators/test_ccsds.pyfor the rejection and for the boundary (exactly six bytes accepted). - Once construction guarantees a full header, the short-input branch added to
__str__in #282 becomes unreachable and can be removed along with its test.
This is a behavior change for any caller that currently builds short CCSDSPacketBytes instances, so it should be called out in the changelog and land in a minor or major release per the project's semantic versioning practice, not a patch.
Alternative Considered
Keep construction permissive and have each header property raise ValueError with a message instead of IndexError. This avoids the compatibility change but leaves the two bytes subclasses inconsistent and spreads the length check across seven properties.
Related
- #276 / #282: made
__str__safe for short input on the error-reporting path.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in space_packet_parser/generators/ccsds.py and compare CCSDSPacketBytes construction with UDPPacketBytes in generators/udp.py. Run tests/unit/test_generators/test_ccsds.py, including the truncated-final-packet path, rejection of short inputs, and the six-byte boundary case. Done means short construction has a clear documented error, valid boundary input works, existing warning behavior remains, and the changelog records the compatibility change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 70/100