Repository navigation
Compare network addresses in is_subnet_of - #235
Open
amalsaidov wants to merge 1 commit into
Open
amalsaidov wants to merge 1 commit into
amalsaidov wants to merge 1 commit into
Conversation
is_subnet_of compared the raw stored addresses (ip()) of the two networks instead of their masked network addresses. Because a network can be constructed with host bits set (e.g. "10.1.0.0/15" stores 10.1.0.0 even though its network address is 10.0.0.0), the raw-address comparison let a smaller network be reported as a supernet of a larger one that happened to share the stored address, and made two networks denoting the same range fail to be subnets of each other. Use network() for both operands so the comparison reflects the actual address ranges, matching the already-correct broadcast()/network()/ contains() behaviour. Fixes the same bug for IPv6. Fixes achanda#152
achanda
reviewed
Dec 2, 2026
| fn test_is_subnet_of_uses_network_address() { | ||
| let smaller: Ipv4Network = "10.1.0.0/16".parse().unwrap(); | ||
| let larger: Ipv4Network = "10.1.0.0/15".parse().unwrap(); | ||
| // 10.1.0.0/15 has network address 10.0.0.0, so the /16 is not inside it, |
Owner
There was a problem hiding this comment.
This seems wrong? the /15 does contain the /16
This branch has not been deployed
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.
Problem
Ipv4Network::is_subnet_of(and the IPv6 equivalent) compares the raw stored addresses of the two networks:A network can be constructed with host bits set — e.g.
"10.1.0.0/15"stores10.1.0.0even though its network address is10.0.0.0. Comparingip()instead of the masked network address therefore produces wrong results:It also makes two networks that denote the same range (one with non-zero host bits) fail to be subnets of each other.
This is the bug reported in #152.
Fix
Use
network()for both operands, matching the already-correct masking innetwork(),broadcast()andcontains(). Applied to bothIpv4NetworkandIpv6Network.Tests
Added
test_is_subnet_of_uses_network_addressto the IPv4 and IPv6 test modules (fail before, pass after). Full suite green (cargo test),cargo clippy --all-targetsclean.Fixes #152