Skip to content

Commit

Permalink
Merge pull request #16 from sticnarf/fix-add-to-zero
Browse files Browse the repository at this point in the history
Fix incorrect result when inserting to all zero network
  • Loading branch information
sticnarf authored Nov 4, 2021
2 parents abbe413 + 03118ea commit e5172ff
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,16 @@ where
}

fn insert(&mut self, network: N) {
if self.root.is_none() {
self.root = Some(IpTrieNode::new())
}

let mut node = self.root.as_mut().unwrap() as *mut IpTrieNode; // The current node
// The current node
let mut node = if let Some(root) = &mut self.root {
if root.is_leaf() {
// Insert into all-zero network has no effect.
return;
}
root as *mut IpTrieNode
} else {
self.root.insert(IpTrieNode::new()) as *mut IpTrieNode
};

unsafe {
let bits = network.prefix_bits();
Expand Down Expand Up @@ -1753,4 +1758,12 @@ mod tests {
ip_range.remove(network);
assert!(ip_range.iter().next().is_none());
}

#[test]
fn add_to_all_zeros() {
let mut ip_range: IpRange<Ipv4Net> = IpRange::new();
ip_range.add("0.0.0.0/0".parse().unwrap());
ip_range.add("127.0.0.1/32".parse().unwrap());
assert!(ip_range.contains_network("0.0.0.0/0"));
}
}

0 comments on commit e5172ff

Please sign in to comment.