scott-griffiths / bitstring

A Python module to help you manage your bits
https://bitstring.readthedocs.io/en/stable/index.html
MIT License
401 stars 67 forks source link

bitstring 4.1.3 no longer allows packing and reading bytes without a defined length #303

Closed BVollmerhaus closed 8 months ago

BVollmerhaus commented 8 months ago

Issue

After updating bitstring from 4.1.2 to 4.1.3, the following code no longer functions as expected, instead crashing with a bitstring.exceptions.CreationError: Token 'bytes' not supplied with a length. exception:

bitstring.pack('bytes', b'abcd')

Likewise, reading an arbitrary number of bytes from the current position to the end of the stream now crashes with a bitstring.exceptions.InterpretError: No length given for dtype 'bytes', and meta type is not fixed length.:

remainder: bytes = some_stream.read('bytes')

Both cases worked fine with bitstring 4.1.2, so this seems like either an unintentional or undocumented breaking change. When packing a more complex format, it can often be useful to directly include some existing bytes of arbitrary length in one pack() operation, hence why I relied on bytes not requiring a length:

bitstring.pack('uint:2, bool, bytes, uint:4', ...)

Workarounds

  1. Downgrade to bitstring 4.1.2.

  2. At least for packing, a possible workaround is to pack the first part of the stream, append the desired bytes, and ā€“ if needed ā€“ pack the rest separately:

stream: BitStream = bitstring.pack('uint:2, bool', ...)
stream.append(some_bytes)
stream.append(bitstring.pack('uint:4', ...))
scott-griffiths commented 8 months ago

Hi, thanks for the bug report.

Yes, this is a bug that appears to have slipped through - there was a bit of a refactor and it seems this case didn't have a unit test. šŸ˜ž

I'll make a fix, add a test and make another point release.

scott-griffiths commented 8 months ago

Should all be working again in 4.1.4. Let me know if you have any more issues, and thanks again for the bug report.