Skip to content
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

Eliminate a handful of unwraps from the codebase #4622

Merged
merged 4 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/seccompiler/src/seccompiler_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,14 @@ fn build_arg_parser() -> ArgParser<'static> {
}

fn get_argument_values(arguments: &ArgumentsBag) -> Result<Arguments, SeccompError> {
let arch_string = arguments.single_value("target-arch");
if arch_string.is_none() {
let Some(arch_string) = arguments.single_value("target-arch") else {
return Err(SeccompError::MissingTargetArch);
}
let target_arch: TargetArch = arch_string.unwrap().as_str().try_into()?;
};
let target_arch: TargetArch = arch_string.as_str().try_into()?;

let input_file = arguments.single_value("input-file");
if input_file.is_none() {
let Some(input_file) = arguments.single_value("input-file") else {
return Err(SeccompError::MissingInputFile);
}
};

let is_basic = arguments.flag_present("basic");
if is_basic {
Expand All @@ -133,7 +131,7 @@ fn get_argument_values(arguments: &ArgumentsBag) -> Result<Arguments, SeccompErr

Ok(Arguments {
target_arch,
input_file: input_file.unwrap().to_owned(),
input_file: input_file.to_owned(),
// Safe to unwrap because it has a default value
output_file: arguments.single_value("output-file").unwrap().to_owned(),
is_basic,
Expand Down
14 changes: 4 additions & 10 deletions src/vmm/src/devices/virtio/block/virtio/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,14 @@ impl BlockMetricsPerDevice {
/// lock is always initialized so it is safe the unwrap
/// the lock without a check.
pub fn alloc(drive_id: String) -> Arc<BlockDeviceMetrics> {
if METRICS.read().unwrap().metrics.get(&drive_id).is_none() {
Arc::clone(
METRICS
.write()
.unwrap()
.metrics
.insert(drive_id.clone(), Arc::new(BlockDeviceMetrics::new()));
}
METRICS
.read()
.unwrap()
.metrics
.get(&drive_id)
.unwrap()
.clone()
.entry(drive_id)
.or_insert_with(|| Arc::new(BlockDeviceMetrics::default())),
)
}
}

Expand Down
14 changes: 4 additions & 10 deletions src/vmm/src/devices/virtio/net/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,14 @@ impl NetMetricsPerDevice {
/// lock is always initialized so it is safe the unwrap
/// the lock without a check.
pub fn alloc(iface_id: String) -> Arc<NetDeviceMetrics> {
if METRICS.read().unwrap().metrics.get(&iface_id).is_none() {
Arc::clone(
METRICS
.write()
.unwrap()
.metrics
.insert(iface_id.clone(), Arc::new(NetDeviceMetrics::new()));
}
METRICS
.read()
.unwrap()
.metrics
.get(&iface_id)
.unwrap()
.clone()
.entry(iface_id)
.or_insert_with(|| Arc::new(NetDeviceMetrics::default())),
)
}
}

Expand Down
21 changes: 8 additions & 13 deletions src/vmm/src/devices/virtio/vhost_user_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,14 @@ impl VhostUserMetricsPerDevice {
/// lock is always initialized so it is safe the unwrap
/// the lock without a check.
pub fn alloc(drive_id: String) -> Arc<VhostUserDeviceMetrics> {
if METRICS.read().unwrap().metrics.get(&drive_id).is_none() {
METRICS.write().unwrap().metrics.insert(
drive_id.clone(),
Arc::new(VhostUserDeviceMetrics::default()),
);
}
METRICS
.read()
.unwrap()
.metrics
.get(&drive_id)
.unwrap()
.clone()
Arc::clone(
METRICS
.write()
.unwrap()
.metrics
.entry(drive_id)
.or_insert_with(|| Arc::new(VhostUserDeviceMetrics::default())),
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vmm/src/devices/virtio/vsock/unix/muxer_killq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl MuxerKillQ {
pub fn pop(&mut self) -> Option<ConnMapKey> {
if let Some(item) = self.q.front() {
if Instant::now() > item.kill_time {
return Some(self.q.pop_front().unwrap().key);
return self.q.pop_front().map(|entry| entry.key);
}
}
None
Expand Down
8 changes: 2 additions & 6 deletions src/vmm/src/dumbo/tcp/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,13 +714,9 @@ impl Connection {

// We check this here because if a valid payload has been received, then we must have
// set enqueue_ack = true earlier.
if payload_len > 0 {
if let Some(payload_len) = NonZeroUsize::new(payload_len.into()) {
buf[..payload_len.into()].copy_from_slice(s.payload());
// The unwrap is safe because payload_len > 0.
return Ok((
Some(NonZeroUsize::new(payload_len.into()).unwrap()),
recv_status_flags,
));
return Ok((Some(payload_len), recv_status_flags));
}
}

Expand Down
28 changes: 14 additions & 14 deletions src/vmm/src/vmm_config/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ impl NetBuilder {
&mut self,
netif_config: NetworkInterfaceConfig,
) -> Result<Arc<Mutex<Net>>, NetworkInterfaceError> {
let mac_conflict = |net: &Arc<Mutex<Net>>| {
let net = net.lock().expect("Poisoned lock");
// Check if another net dev has same MAC.
netif_config.guest_mac.is_some()
&& netif_config.guest_mac.as_ref() == net.guest_mac()
&& &netif_config.iface_id != net.id()
};
// Validate there is no Mac conflict.
// No need to validate host_dev_name conflict. In such a case,
// an error will be thrown during device creation anyway.
if self.net_devices.iter().any(mac_conflict) {
return Err(NetworkInterfaceError::GuestMacAddressInUse(
netif_config.guest_mac.unwrap().to_string(),
));
if let Some(ref mac_address) = netif_config.guest_mac {
let mac_conflict = |net: &Arc<Mutex<Net>>| {
let net = net.lock().expect("Poisoned lock");
// Check if another net dev has same MAC.
Some(mac_address) == net.guest_mac() && &netif_config.iface_id != net.id()
};
// Validate there is no Mac conflict.
// No need to validate host_dev_name conflict. In such a case,
// an error will be thrown during device creation anyway.
if self.net_devices.iter().any(mac_conflict) {
return Err(NetworkInterfaceError::GuestMacAddressInUse(
mac_address.to_string(),
));
}
}

// If this is an update, just remove the old one.
Expand Down
6 changes: 3 additions & 3 deletions src/vmm/src/vstate/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ impl Vm {
}
guest_mem
.iter()
.enumerate()
.try_for_each(|(index, region)| {
.zip(0u32..)
.try_for_each(|(region, slot)| {
let memory_region = kvm_userspace_memory_region {
slot: u32::try_from(index).unwrap(),
slot,
guest_phys_addr: region.start_addr().raw_value(),
memory_size: region.len(),
// It's safe to unwrap because the guest address is valid.
Expand Down