Skip to content

fix: ip_count overflow panic on full address space (/0 prefixes)#7

Merged
digizeph merged 1 commit into
mainfrom
fix/ip-count-overflow
Jul 9, 2026
Merged

fix: ip_count overflow panic on full address space (/0 prefixes)#7
digizeph merged 1 commit into
mainfrom
fix/ip-count-overflow

Conversation

@digizeph

@digizeph digizeph commented Jul 9, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #5

ip_count panicked in debug builds (and silently wrapped in release) when the trie contained a /0 prefix (0.0.0.0/0 or ::/0), because 2u32.pow(32) and 2u128.pow(128) overflow.

Changes

  • Return type: (u32, u128) → new IpCount struct
    • ipv4: u64 — holds the full 2³² IPv4 address space
    • ipv6: Option<u128>None when the entire 2¹²⁸ IPv6 space is covered (too large for u128)
  • Arithmetic: replaced pow() with checked_shl / checked_add to detect overflow without panicking
  • Updated doc examples, unit tests, README, CHANGELOG
  • Added test cases for 0.0.0.0/0, ::/0, and two /1 halves covering all of IPv6
  • Bumped version to 0.3.1

Notes

  • IpCount is a transient stack return value — the trie storage (PrefixMap) is unchanged, so there is no in-memory storage cost increase
  • This is a breaking API change (return type), hence the minor version bump

Checklist

  • cargo test --all-features passes
  • cargo fmt --check passes
  • CHANGELOG updated
  • Version bumped

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.
@digizeph digizeph merged commit 46b616a into main Jul 9, 2026
1 check 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.

ip_count panics on complete sets

1 participant