refactor(dpi/snmp): collapse find_pdu_type match, drop unreachable Unknown variant#279
Merged
Merged
Conversation
…known variant `find_pdu_type` gated its match with `if (PDU_GET_REQUEST..=PDU_REPORT).contains(&byte)` and then mapped every value in that range explicitly — leaving `Unknown(other)` as the only catch-all arm, which the outer check made unreachable. Fold the gate into the match using `_ => continue` so non-PDU bytes are skipped in a single pass. With the construction site gone, `SnmpPduType::Unknown(u8)` becomes truly dead (the compiler raised `dead_code` on it). Drop the variant and its Display arm rather than leaving a phantom for a path the parser never produces. Add tests for `find_pdu_type` that exercise the loop directly: - skip a leading INTEGER (request-ID) and land on `GetBulkRequest` - return None when no PDU tag is present - enforce the 50-byte search cap (PDU at offset 60 must not be found) The first one is the most interesting — it pins down the contract that the scan tolerates header noise between the community string and the PDU tag, which is the real-world layout for v1/v2c packets.
Owner
|
@0xghost42 Thanks a lot for your PR. This looks good to me! |
This was referenced May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
find_pdu_typegates its PDU-tag lookup with an outer range check, then dispatches inside with a match that includes anUnknown(other)catch-all:The match arms cover all nine consecutive constants
0xA0..=0xA8explicitly, and the outercontainsexcludes anything outside that range — so theUnknown(other)arm has no value it can ever match.Changes
Inline the gate into the match.
_ => continueskips non-PDU bytes so the loop scans through header noise in a single pass without the redundantcontainscheck.Drop
SnmpPduType::Unknown(u8). With the construction site removed,cargo clippyflags the variant as dead code. The parser never produces it, and the previous Display armUNKNOWN({})was only reachable from a code path that didn't exist — deleting it keeps the public type honest about what values it can hold.Why it matters
Reportwas a late addition for a reason), the fix lives in the match — there's no Unknown-shaped pretense that we already handle it.clippy::dead_codewaiver the codebase didn't actually have but would have needed to silence.Tests
Added three tests for
find_pdu_typethat exercise the loop directly:test_find_pdu_type_skips_non_pdu_bytes— leading INTEGER (request-ID) bytes followed byPDU_GET_BULK_REQUEST; the scan must skip the noise and land on the tag. This is the real-world v1/v2c layout: community string is parsed byanalyze_snmp, thenfind_pdu_typeis handed the remainder which starts with non-PDU bytes.test_find_pdu_type_returns_none_when_no_pdu— payload with no PDU tag returnsNone.test_find_pdu_type_caps_search_at_50_bytes— PDU tag at offset 60 must not be found (locks the existing scan cap).Full suite stays green (327 passing).
No wire-format behavior change for any input the parser already accepted.