Skip to content

Commit

Permalink
refactor: Replace addr_of_mut with MaybeUninit::as_mut_ptr
Browse files Browse the repository at this point in the history
Use MaybeUninit::as_mut_ptr as it's a more safer option to
avoid any potential bugs with respect to uninitialized memory.

Signed-off-by: Vaishali Thakkar <[email protected]>
  • Loading branch information
v-thakkar committed Sep 5, 2024
1 parent a320efa commit 4547340
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 134 deletions.
9 changes: 5 additions & 4 deletions crates/krata/src/ethtool.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
mem::MaybeUninit,
os::fd::{AsRawFd, FromRawFd, OwnedFd},
ptr::addr_of_mut,
};

use anyhow::Result;
Expand Down Expand Up @@ -70,9 +70,10 @@ impl EthtoolHandle {

fn set_value(&mut self, interface: &str, cmd: u32, value: u32) -> Result<()> {
let mut ifreq = EthtoolIfreq::new(interface);
let mut value = EthtoolValue { cmd, data: value };
ifreq.set_value(addr_of_mut!(value) as *mut libc::c_void);
let result = unsafe { ioctl(self.fd.as_raw_fd(), SIOCETHTOOL, addr_of_mut!(ifreq) as u64) };
let mut value = MaybeUninit::new(EthtoolValue { cmd, data: value });
ifreq.set_value(value.as_mut_ptr() as *mut libc::c_void);
let mut ifreq = MaybeUninit::new(ifreq);
let result = unsafe { ioctl(self.fd.as_raw_fd(), SIOCETHTOOL, ifreq.as_mut_ptr() as u64) };
if result == -1 {
return Err(std::io::Error::last_os_error().into());
}
Expand Down
Loading

0 comments on commit 4547340

Please sign in to comment.