Skip to content

Commit 1ccaa71

Browse files
committed
fix(protocol): raise StructError from _make_pack for any size (#2291)
Problem: for a size with no struct format code, _make_pack called int.to_bytes outside any handler, so a value out of range, a non-integer or a bad size leaked OverflowError, AttributeError or ValueError instead of the documented StructError; _make_index(..., pack=True) leaked them too. Fix: raise StructError from the errors of int.to_bytes, as the struct.pack path does from struct.error, and document StructError on _make_index. Test: test_make_pack_raises_struct_error_for_every_size.
1 parent 7b57bb0 commit 1ccaa71

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎pcapkit/protocols/protocol.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,12 @@ def _make_pack(cls, integer: 'int', *, size: 'int' = 1,
18861886

18871887
if kind is None:
18881888
end = 'little' if lilendian else 'big' # type: Literal['little', 'big']
1889-
buf = integer.to_bytes(size, end, signed=signed)
1889+
# ``int.to_bytes`` raises these where ``struct.pack`` raises
1890+
# ``struct.error`` for the other sizes (#2291).
1891+
try:
1892+
buf = integer.to_bytes(size, end, signed=signed)
1893+
except (AttributeError, OverflowError, TypeError, ValueError) as error:
1894+
raise StructError(f'{cls.__name__}: pack failed') from error
18901895
else:
18911896
try:
18921897
fmt = f'{endian}{kind}'
@@ -1964,6 +1969,8 @@ def _make_index(cls, name: 'str | int | StdlibEnum | AenumEnum', default: 'Optio
19641969
Raises:
19651970
ProtocolNotImplemented: If ``name`` is **NOT** in ``namespace``
19661971
and ``default`` is :data:`None`.
1972+
StructError: If ``pack`` is :data:`True` and the index failed to
1973+
pack (see :meth:`_make_pack`).
19671974
19681975
Note:
19691976
If ``name`` is a :obj:`str` and ``namespace`` is :data:`None`,

‎tests/protocols/test_protocol_base_unit.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,29 @@ def test_lookup_next_layer_reads_the_fallback_without_recording_it(self) -> None
460460
self.assertIsInstance(proto._import_next_layer(99, 3), Raw)
461461
self.assertEqual(set(DummyProtocol.__proto__), {7, 8})
462462

463+
def test_make_pack_raises_struct_error_for_every_size(self) -> None:
464+
# #2291: a size with no struct format code leaked the errors of
465+
# int.to_bytes instead of the StructError the other sizes raise.
466+
DummyProtocol, _, _ = self._make_protocol_class()
467+
from pcapkit.utilities.exceptions import StructError
468+
469+
cases = (
470+
(2 ** 24, {'size': 3}), # too big
471+
(-1, {'size': 3}), # negative, unsigned
472+
(2 ** 23, {'size': 3, 'signed': True}), # too big, signed
473+
(1.5, {'size': 3}), # not an integer
474+
(None, {'size': 5}), # not an integer
475+
(1, {'size': 0}), # no room at all
476+
(1, {'size': -1}), # negative size
477+
)
478+
for integer, kwargs in cases:
479+
with self.subTest(integer=integer, **kwargs):
480+
with self.assertRaises(StructError):
481+
DummyProtocol._make_pack(integer, **kwargs)
482+
with self.assertRaises(StructError):
483+
DummyProtocol._make_index(2 ** 24, pack=True, size=3)
484+
self.assertEqual(DummyProtocol._make_pack(-2, size=3, signed=True), b'\xff\xff\xfe')
485+
463486
def test_make_payload_branches(self) -> None:
464487
DummyProtocol, DummyData, _ = self._make_protocol_class()
465488
from pcapkit.protocols.misc.null import NoPayload

0 commit comments

Comments
 (0)