fix(corekit): raise ProtocolError for a NumberField negative bit_length (#831) - #834
Conversation
…th (#831) `NumberField.__init__` shifted by `bit_length` eagerly -- `(1 << bit_length) - 1` -- so a negative `bit_length` raised a bare, uncatchable `ValueError` instead of a pcapkit error. #829 guarded the sibling shift in `__call__` (a negative resolved `length`) but left this one, at numbers.py:88, open. - `__init__` now raises `ProtocolError` for a negative `bit_length`, naming `bit_length=` rather than a resolved byte length, since that is what is wrong. - `__call__`'s existing negative-`length` guard now runs unconditionally instead of only inside the `bit_length`-not-supplied branch, so a field combining a fixed `bit_length` with a callable `length` that resolves negative raises the same message as one with no `bit_length` at all, instead of a different one from `FieldBase.length` later. `coverage run -m pytest` on tests/corekit/ and tests/protocols/application/: numbers.py stays at 100% (142->144 stmts, 0 missed both). The pinned negative-length assertion in test_http_unit.py is untouched.
|
GOOD TO GO — cross-review (opus, independent tree, One correction, which I re-derived myself: the description undersells the delta. It is not only "a leaked So a Unreachable in-tree: the only |
Closes #831
make pylint,make mypy,make isort)make testpasses, and a test case covers the changeWhat is the purpose of your pull request?
fix— corrects a defectDescription
#829 guarded the eager
1 << lengthshift inNumberField.__call__; thesibling shift in
__init__—(1 << bit_length) - 1— was still open, soNumberField(length=4, bit_length=-1)raised a bareValueErrorinsteadof a catchable
ProtocolError.Also closes the second half:
__call__'s negative-lengthguard used tosit only inside the
bit_length-not-supplied branch, so a field with afixed
bit_lengthand a callablelengthresolving negative produced adifferent
ProtocolErrormessage (template='...-1s', fromFieldBase.length) than one with nobit_lengthat all(
length=...). The guard now runs unconditionally, so both raise thesame message. No in-tree call site combines the two (verified against
vlan.py's threebit_lengthfields, which all take positive literals).New tests in
tests/corekit/test_fields_numbers_negative_length.py(
NegativeBitLengthTests), shown failing on stock5576708d4before thefix.
coverage runontests/corekit/+tests/protocols/application/:numbers.pystays at 100% (142→144 stmts). The pinned negative-lengthassertion in
test_http_unit.pyis untouched.