Repository navigation
fix: prevent arithmetic panics on clock jumps and oversized packets - #101
Conversation
lance0
left a comment
There was a problem hiding this comment.
Review: PR #101 — fix: prevent arithmetic panics on clock jumps and oversized packets
Verdict: COMMENT (changes are correct and safe to merge as-is; one scope gap worth a follow-up).
Reviewed against the PR branch (fix/arithmetic-safety) and the three changed files. I verified each of the six review-focus points called out in the assignment.
Findings
-
[Medium] Incomplete coverage of #19 —
engine.rs:1438—src/trace/engine.rs,poll_ipv6_echo_reply, line 1438 still usesInstant::now().duration_since(probe.sent_at). This is the identical panic pattern this PR fixes inreceiver.rs.poll_ipv6_echo_replyis a live production path (single-targetProbeEnginepolling ICMPv6 Echo Reply on Linux, since the kernel delivers Echo Reply only to the sending socket). A backward monotonic-clock jump (VM migration / suspend-resume) panics this thread exactly as it would the receiver thread. The PR body scopes #19 to "receiver RTT calculation," but the issue itself is the generalInstant::now().duration_since(probe.sent_at)pattern; this site should be converted too. Recommend a one-line follow-up:Instant::now().saturating_duration_since(probe.sent_at). Not a merge blocker for this PR (the receiver sites it does touch are correct), but #19 should not be considered fully closed while this site remains. -
[Low] No test for the
saturating_duration_sinceconversion — The three new tests cover #13 (test_ix_cache_future_fetched_at_does_not_panic) and #23 (test_build_ipv4_packet_max_size,test_build_ipv4_packet_oversized_panics), but none exercise the #19saturating_duration_sincepaths inreceiver.rs. Forcing a backwardInstantdirectly is awkward, but the RTT computation could be extracted behind a small pure helper taking explicitInstant/Durationargs so the zero-RTT degradation path is unit-testable. Flagging as a coverage gap, not a blocker. -
[Verified] Both
receiver.rsduration_sincesites converted — Confirmed: line 233 (RTT calc) and line 435 (timeout cleanup in thepending.retainclosure) both usesaturating_duration_since. No remaining.duration_since(calls onInstantinreceiver.rs. Theduration_since(SystemTime::UNIX_EPOCH)calls (used to derive unix seconds withunwrap_or_default()) are correctly left as-is — they are a different pattern and already guarded. -
[Verified] All three
saturating_subsites inix.rsconverted, no missed sites — Lines 87 (is_expired), 317 (backoff check), and 569 (get_cache_statuscache-expiry display) all usesaturating_sub. I scanned for any remaining plain-subtraction on the sameu64timestamps (fetched_at,last_failure/last_fail,now); the only remainingnow - Xoccurrences (lines 822, 882, 886, 883, 887) are in#[cfg(test)]test setup/assertions, not production. Production coverage is complete. -
[Verified]
build_ipv4_packetassert is correctly ordered and edge-safe —total_payload = IPV4_HEADER_SIZE + transport.len()is computed asusize(≥32-bit on all supported targets) before theas u16cast, so there is no wrap preceding the bounds check. The asserttotal_payload <= u16::MAX as usizethen gates the cast. Edge cases checked:- Empty transport (0 bytes):
total_payload = 20, assert passes,total_len = 20(valid header-only packet). - Max transport (65515 bytes):
total_payload = 65535, assert passes,total_len = 65535. - Oversized (65516 bytes):
total_payload = 65536, assert fails with the descriptive message.
Theshould_panictest correctly pins the message substring.
- Empty transport (0 bytes):
-
[Verified / Positive] assert placement protects all callers —
build_ipv4_packetis called fromengine.rs(5×: ICMP/UDP/TCP send + PMTUD) andreceiver.rspaths, so the single assert in the shared builder covers every packet-construction entry point; no duplication needed. -
[Nit] Degradation behavior worth a comment — When
saturating_duration_sinceclamps a backward clock toDuration::ZERO, the affected probe's RTT is reported as 0. This is strictly better than panicking the thread, but a single 0-ms sample can slightly skew min/avg RTT statistics for that one probe. Benign and rare (requires a monotonic-clock regression); a one-line comment at the call site noting "RTT may read 0 on a backward clock jump; preferred over panicking the receiver" would make the intent explicit for future readers.
Summary
The three changes the PR actually makes are correct, minimal, and well-targeted. saturating_sub coverage in ix.rs is complete; the build_ipv4_packet assert is correctly ordered and handles the empty/max/oversized edges; both receiver.rs sites are converted. The one substantive concern is that #19 is only partially fixed — the same Instant::now().duration_since(probe.sent_at) panic remains in engine.rs:1438. Recommend merging this PR and opening a tiny follow-up (or amending here) to convert that one line so #19 is fully closed.
- Use saturating_duration_since in receiver RTT and timeout calculations to prevent panic if monotonic clock moves backward (VM migration, hypervisor time-sync) (#19) - Use saturating_sub in PeeringDB cache expiry/backoff checks to prevent underflow panic if fetched_at is in the future (clock jump) (#13) - Add assert in build_ipv4_packet to catch IPv4 total_len u16 wrap on oversized transport payloads instead of silently truncating (#23) All 499 tests pass (including 3 new); clippy clean.
7affc4e to
42693ae
Compare
lance0
left a comment
There was a problem hiding this comment.
Follow-up after rebase
Confirmed the rebased PR addresses the Medium finding from my earlier review: src/trace/engine.rs:1441 now uses Instant::now().saturating_duration_since(probe.sent_at) (the poll_ipv6_echo_reply RTT path). I verified there are no remaining Instant::now().duration_since / .duration_since(probe...) call sites in either engine.rs or receiver.rs. Issue #19 is now fully closed across all three RTT/timeout sites (receiver.rs L233 + L435, engine.rs L1441).
No further issues. The saturating_sub sites in ix.rs and the build_ipv4_packet assert are unchanged and remain correct per my prior review. Ship it.
Summary
Prevents arithmetic panics caused by clock jumps (VM migration, hypervisor time-sync) and oversized packet construction.
Changes
saturating_duration_sinceinstead ofduration_sincein receiver RTT calculation (L261), timeout cleanup (L466), and engine.rs IPv6 Echo Reply polling (L1441). Prevents receiver/engine-thread panic if the monotonic clock moves backward relative to a storedInstant. (Rebase picked up the engine.rs site flagged in review — now all threeInstant::now().duration_sincesites are converted.)saturating_subin PeeringDB cache expiry (is_expired), cache status display, and backoff period checks. Preventsu64underflow panic iffetched_atorlast_failuretimestamp is somehow in the future (clock jump, corrupt cache).assert!inbuild_ipv4_packetto catch IPv4total_lenu16wrap on oversized transport payloads instead of silently truncating the length field.Testing
cargo clippy --all-targets -- -D warningscleantest_build_ipv4_packet_max_size(exactly u16::MAX),test_build_ipv4_packet_oversized_panics(should_panic),test_ix_cache_future_fetched_at_does_not_panicRisk
Low — mechanical saturating/checked arithmetic replacements, no logic changes. Rebased onto current master; conflict in
receiver.rstimeout-cleanup block resolved by keeping master's refactored structure and applyingsaturating_duration_since.