Skip to content

Code hygiene: drop unused csv crate, document unsafe blocks, audit dead_code allows - #182

Merged
lance0 merged 7 commits into
masterfrom
chore/code-hygiene
Nov 14, 2026
Merged

lance0 merged 7 commits into
masterfrom
chore/code-hygiene

Conversation

@lance0

@lance0 lance0 commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Hygiene only. Nothing here changes runtime behavior. The one API change is the removal of the public ttl::state::ProbeResult, which is breaking for library users and is listed under Removed in the CHANGELOG.

1. Remove the unused csv crate

CSV export is written by hand in src/export/csv.rs. The pub use csv::* line in src/export/mod.rs re-exports that local module, not the crate. Nothing in src, tests, build.rs or examples refers to the crate, and cargo's unused_dependencies lint flags it on master. The entry is gone from Cargo.toml, and Cargo.lock drops csv and csv-core.

2. SAFETY comments and a lint

Each unsafe block now has a // SAFETY: comment that names the invariant it depends on: fd validity, live pointer/length pairs in recvmsg, zeroable C structs, the sockaddr family casts, and the cmsg bounds that read_cmsg_ttl requires. This covers the Linux, macOS, FreeBSD, NetBSD and Windows cfg-gated blocks.

[lints.clippy] undocumented_unsafe_blocks = "warn" is now set in Cargo.toml, so the -D warnings clippy runs in CI (Linux, macOS, FreeBSD, Windows) enforce it.

The comments in the set_dont_fragment setsockopt blocks sit inside the block. That keeps them off the lines #176 edits, so the two PRs merge cleanly.

The root-only on-wire test in rawip.rs now reads through socket2's safe Read impl. It no longer reinterprets a MaybeUninit buffer.

I kept libc::if_nametoindex. No existing safe API matches what it does.

3. allow(dead_code) audit

  • Deleted (unused in every cfg):

    • ttl::state::ProbeResult: public, so this is a breaking library change (CHANGELOG Removed).
    • create_udp_send_socket and create_udp_dgram_socket_bound
    • udp::IPPROTO_UDP: the engine uses rawip::IPPROTO_UDP and correlate.rs has its own.
    • IxLookup::prefix_count
    • SocketCapability::Dgram: never constructed on any platform.
    • Theme::border_focused: never read. The theme.rs diff also includes rustfmt re-aligning trailing comments.
    • A stale allow on IPV6_NH_HOP_BY_HOP.
  • #[cfg(test)] (used only by tests):

    • build_tcp_syn, TCP_SRC_PORT and DEFAULT_TCP_PAYLOAD
    • build_udp_payload
    • The raw-IPv6 parser chain in correlate.rs: parse_icmp_response_v6, skip_ipv6_extension_headers, parse_icmp_error_payload_v6_with_mtu and IPV6_NH_ICMPV6.

    I gated the chain with an attribute instead of deleting it, so this PR stays clear of the parser code that PMTUD DF cleanup, PMTUD tick lock, and UDP header-only ICMP quotes #176 adds right after parse_icmp_error_payload_v6_with_mtu. It can be deleted in a follow-up, along with the third case in its test.

  • cfg_attr: InterfaceInfo::index is read only by the macOS bind path.

  • Kept, with a reason:

    • Public library API that the binary doesn't call: export_json_string, generate_report_string, ResponderStats::record_timeout, FlowPathStats::new/loss_pct and Hop::record_response.
    • BatchedResponse::responder_scope_id: never read. Deleting it is a logic edit in receiver.rs.
    • The eight module-level allows in lib.rs. The lib compiles those modules only because config/export/state depend on them. The bin build still catches dead code in them. I also expanded the comment there.

Testing

Not verified

  • macOS, FreeBSD and Windows are checked only by CI. NetBSD has no CI job; its only NetBSD-specific unsafe block is the same setsockopt pattern as the Linux one.
  • I did not run the root-only rawip on-wire test locally. CI's Linux job runs it under sudo.

CSV export is written by hand in src/export/csv.rs; the csv crate was
never referenced. The 'pub use csv::*' in src/export/mod.rs re-exports
the local module, not the crate.
Add a SAFETY comment to each unsafe block naming the invariant it relies
on (fd validity, buffer pointers and lengths, zeroable C structs, the cmsg
bounds read_cmsg_ttl needs) and enable clippy's
undocumented_unsafe_blocks lint crate-wide so new blocks need one too.

The root-only on-wire test in rawip.rs now reads through socket2's safe
Read impl instead of reinterpreting a MaybeUninit buffer.
Delete items unused in every cfg: ProbeResult (public, listed under
Removed), create_udp_send_socket, create_udp_dgram_socket_bound,
IxLookup::prefix_count, SocketCapability::Dgram, Theme::border_focused.
Gate test-only helpers with #[cfg(test)] (build_tcp_syn and its consts,
build_udp_payload, the raw-IPv6 ICMP parser chain in correlate.rs). Give
public library API kept for lib users a reason, and narrow
InterfaceInfo::index to non-macOS.
The cmsg_len >= CMSG_LEN(0) + 1 check only makes data_len nonzero. The bytes are readable because the kernel (or the test) wrote cmsg_len to end inside the control buffer and the caller skips MSG_CTRUNC.
The engine imports rawip::IPPROTO_UDP and correlate.rs has its own, so this one is dead on every platform.
# Conflicts:
#	CHANGELOG.md
#	src/lookup/ix.rs
@lance0
lance0 marked this pull request as ready for review November 14, 2026 21:03
@lance0
lance0 merged commit 30364c1 into master Nov 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant