Repository navigation
fix(pcapng): resolve byteorder safely in _make_pcapng_options for raw bytes options - #2355
Qiu-LingYan wants to merge 4 commits into
Conversation
… bytes options _make_pcapng_options read self._byte for raw bytes option items, but only _make_block_shb and the systemd journal entry maker ever set that attribute, so making any other block (IDB, DSB, ...) with a raw bytes option raised AttributeError. Fall back to the bound section context's byteorder, or the system byteorder when no context is available. Fixes JarryShaw#2353
|
Review: NEEDS CHANGES at 1. Test 2's second block doesn't parse. It builds a little-endian raw option inside a big-endian section. On a real parse, the option doesn't even round-trip — it comes back with a warning and empty 2. The mutation check found the tests prove less than they claim. All six mutants I tried — byteorder always big, always little, ignoring the context, inverted, dropping the option, replacing it with zeros — still pass both tests. Please replace the 3. Prose. "Used to raise", "now falls back" and "must keep working" are change-relative wording the repo's test prose avoids; and the "SHB-then-block ordering still works" claim in the test docstring and PR body isn't accurate — the two instances are linked only through the shared context, not ordering. Verified independently, and it holds:
One more site with the identical bug, found while reviewing — worth folding into this PR or its own: |
…hten tests _make_nrb_records had the identical self._byte read for raw bytes records in an NRB's records=[...]; resolve it the same way as _make_pcapng_options. Tests now compare the raw-bytes build against the equivalent typed-option build, parse the result back through a real section context and read the option payload, and check a from_data rebuild is byte-exact, in both byte orders. The raw option is encoded in the section's own byte order in the big-endian case.
|
Thanks for the thorough review — all four points addressed in 1eaaf77:
|
|
Review: NEEDS CHANGES at On the options maker, this is reachable: a raw On the records maker, I think this one's genuinely hard to observe from the outside — the decoded type is only ever compared against the all-zero end-record sentinel, which is byte-order invariant either way. If there's no behavioral difference to pin there, that's fine; just flagging it rather than asking for an impossible test. Two more things, worth folding into the same edit:
Everything else held up: no regression against the broader pcapng/docstring/tier-guard suite (230+193 passed, including a clean merge onto current |
A raw if_name option followed by a typed if_name option is rejected only when both decode to the same code, so the rejection pins the resolved byte order in a little-endian section, a big-endian section and without a context. The no-context tests derive their raw bytes from sys.byteorder so they hold on big-endian hosts, and the module docstring states the current behaviour. For the records maker the decoded type is only compared against the all-zero end-record sentinel, which is byte-order invariant, so its byte order is not externally observable; the NRB test pins the produced octets against the typed build.
|
All three points addressed (tests-only commit, production code untouched):
On |
|
Review: GOOD TO GO at Nits, non-blocking: the For context: our own #2369 (also closing #2353) takes a different approach — settles byte order once in |
…tests/protocols/misc
|
Nits addressed in 7e0a4a23 (tests-only):
Also noted your #2369 — settling the byte order once in |
|
Appreciated — thanks for addressing all three rounds thoroughly, and for the generous offer. Taking you up on it: #2369 (our own fix for #2353) is already ready-to-merge and covers a bit more ground (the |
Fixes #2353. Also fixes the identical
self._byteread in_make_nrb_recordsfor raw bytes records in an NRB'srecords=[...], as flagged in the round-1 review.Problem
_make_pcapng_optionsreadsself._bytewhen it encounters a rawbytesitem in anoptions=[...]list, and_make_nrb_recordsdoes the same for rawbytesitems inrecords=[...]. Only_make_block_shband the systemd journal entry maker ever set that attribute, so making any other block (IDB, DSB, EPB, ISB, NRB, PB, CB, ...) with a raw bytes option/record raised:Repro (fails on
main):Fix
Both makers now resolve the byteorder in the same way: use the instance's
_bytewhen set (the SHB and journal paths set it themselves), otherwise the bound section context's byteorder, otherwisesys.byteorder.Tests
tests/protocols/misc/test_pcapng_raw_bytes_option_2353_unit.py— 6 tests / 5 subtests:if_nameoption followed by a typedif_nameoption raisesProtocolErroronly when the raw header decodes to the same code — little-endian section, big-endian section, no context, plus the_byte-first tier (instance reuse after an SHB build) and the host fallback (mock.patch.object(sys, 'byteorder', ...))opt_comment→hello)from_data(info)rebuild for both byte orders7 failed / 1 passed on
main→ 6 passed + 5 subtests on this head.test_pack_keeps_instance_state_1769_unit.py+test_arp_pcapng_hwaddr_octets_unit.pypass (14 passed, 19 subtests); the 3 failures intest_capture_roundtrip_runtime.pyare pre-existing onmainand unrelated.