Bugfix: u8 no longer overflows when there are no borrowed bits.

This commit is contained in:
Luke Else 2023-05-02 07:29:32 +01:00
parent b8e4ff21b0
commit 924142e96c

View File

@ -80,17 +80,21 @@ impl Network {
// We know that this value is >0 at this point // We know that this value is >0 at this point
let num_networks = u8::pow(2, num_borrowed_bits as u32); let num_networks = u8::pow(2, num_borrowed_bits as u32);
// Determine the networking gaps
let network_spacing: u8 = u8::pow(2, 8 - num_borrowed_bits as u32);
//Create the subnets //Create the subnets
let mut networks = vec![IpAddr::from_arr(&base_network)?]; let mut networks = vec![IpAddr::from_arr(&base_network)?];
//Add remaming subnets (-1 is because we have already added the first address) // Determine the networking gaps
for _ in 0..(num_networks-1) { //If there is only 1 network, returning the base network will be sufficient
base_network[most_sig_octet as usize] += network_spacing; if num_networks > 1 {
networks.push(IpAddr::from_arr(&base_network.clone())?); let network_spacing: u8 = u8::pow(2, 8 - num_borrowed_bits as u32);
//Add remaming subnets (-1 is because we have already added the first address)
for _ in 0..(num_networks-1) {
base_network[most_sig_octet as usize] += network_spacing;
networks.push(IpAddr::from_arr(&base_network.clone())?);
}
} }
println!("{:#?}", networks); println!("{:#?}", networks);
Ok(networks) Ok(networks)
} }