fix: ip_count overflow panic on full address space (/0 prefixes)#7
Merged
Conversation
ip_count previously used 2u32.pow(32-prefix_len) and 2u128.pow(128-prefix_len), which overflow when a /0 prefix is present — panicking in debug builds and silently wrapping in release builds (#5). Change return type from (u32, u128) to new IpCount struct: - ipv4: u64 (holds the full 2^32 IPv4 address space) - ipv6: Option<u128> (None when the entire 2^128 IPv6 space is covered) Use checked_shl/checked_add instead of pow to detect overflow without panicking. The trie storage (PrefixMap) is unchanged — IpCount is only a transient return value with no in-memory storage cost increase.
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.
Summary
Fixes #5
ip_countpanicked in debug builds (and silently wrapped in release) when the trie contained a/0prefix (0.0.0.0/0or::/0), because2u32.pow(32)and2u128.pow(128)overflow.Changes
(u32, u128)→ newIpCountstructipv4: u64— holds the full 2³² IPv4 address spaceipv6: Option<u128>—Nonewhen the entire 2¹²⁸ IPv6 space is covered (too large foru128)pow()withchecked_shl/checked_addto detect overflow without panicking0.0.0.0/0,::/0, and two/1halves covering all of IPv6Notes
IpCountis a transient stack return value — the trie storage (PrefixMap) is unchanged, so there is no in-memory storage cost increaseChecklist
cargo test --all-featurespassescargo fmt --checkpasses