Skip to content

fix: fix net.is_IP_in_CIDR with IPv6 #1908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged

fix: fix net.is_IP_in_CIDR with IPv6 #1908

merged 1 commit into from
Apr 2, 2025

Conversation

He1pa
Copy link
Contributor

@He1pa He1pa commented Apr 1, 2025

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

2. What is the scope of this PR (e.g. component or file name):

3. Provide a description of the PR(e.g. more details, effects, motivations or doc link):

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Other

4. Are there any breaking changes?(Y/N) and describe the breaking changes(e.g. more details, motivations or doc link):

  • N
  • Y

5. Are there test cases for these changes?(Y/N) select and add more details, references or doc links:

  • Unit test
  • Integration test
  • Benchmark (add benchmark stats below)
  • Manual test (add detailed scripts or steps below)
  • Other

@He1pa He1pa force-pushed the fix_is_ip_in_CIDR branch from 09a5d84 to d2806f2 Compare April 1, 2025 13:02
Copy link
Contributor

@zong-zhe zong-zhe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zong-zhe
Copy link
Contributor

zong-zhe commented Apr 1, 2025

cc @johngmyers 😄

@coveralls
Copy link
Collaborator

coveralls commented Apr 1, 2025

Pull Request Test Coverage Report for Build 14196324909

Details

  • 0 of 56 (0.0%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.01%) to 70.353%

Changes Missing Coverage Covered Lines Changed/Added Lines %
kclvm/runtime/src/net/mod.rs 0 56 0.0%
Files with Coverage Reduction New Missed Lines %
kclvm/runtime/src/net/mod.rs 5 27.82%
Totals Coverage Status
Change from base Build 14185628298: -0.01%
Covered Lines: 52870
Relevant Lines: 75150

💛 - Coveralls

@zong-zhe zong-zhe merged commit 24686d5 into main Apr 2, 2025
12 checks passed
return None;
}
let ip = IpAddr::from_str(parts[0]).ok()?;
let mask_bits = parts[1].parse::<u32>().ok();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This incorrectly succeeds if ip has bits set beyond mask_bits. For example, 10.11.0.0/8.

IpAddr::from_str(ip).map_err(|_| ())
}

fn _check_ipv4_cidr(ip: Ipv4Addr, cidr_ip: Ipv4Addr, mask_bits: u32) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the cidr crate? I was just working on reimplementing net.parse_cidr using it.

}

fn _check_ipv4_cidr(ip: Ipv4Addr, cidr_ip: Ipv4Addr, mask_bits: u32) -> bool {
let mask = !((1u32 << (32 - mask_bits)) - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not overflow for 0.0.0.0/0?

@@ -5,3 +5,4 @@ cidr1 = net.hosts_in_CIDR("192.168.1.0/24")
cidr2 = net.subnets_from_CIDR("192.168.1.0/24")
cidr3 = net.is_IP_in_CIDR("192.168.1.1", "192.168.1.0/24")
cidr4 = net.is_IP_in_CIDR("192.168.0.1", "192.168.1.0/24")
cidr5 = net.is_IP_in_CIDR('2001:db8::1', "2001:db8::/64")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a LOT more tests:

  • First in CIDR
  • Last in CIDR
  • One before first in CIDR
  • One after last in CIDR
  • 0.0.0.0/0 and ::/0
  • IPv4 in ::/0
  • IPv4 in IPv6 CIDR and vice versa
  • Invalid CIDRs
  • Invalid IPs
  • IPv4 /32 and IPv6 /128

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.

net.is_IP_in_CIDR does not work for IPv6
4 participants