From 924142e96c87fbeb078e8c23421e56f018832b4f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 2 May 2023 07:29:32 +0100 Subject: [PATCH] Bugfix: u8 no longer overflows when there are no borrowed bits. --- src/networking/mod.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/networking/mod.rs b/src/networking/mod.rs index d6f5ccb..4004c6f 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -79,18 +79,22 @@ impl Network { // Determine how many networks available // We know that this value is >0 at this point 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 let mut networks = vec![IpAddr::from_arr(&base_network)?]; - //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())?); + // Determine the networking gaps + //If there is only 1 network, returning the base network will be sufficient + if num_networks > 1 { + 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); Ok(networks) }