Skip to content

Commit

Permalink
fixup: safety
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Aug 20, 2024
1 parent 8fbb73f commit bf2e1b7
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions talpid-net/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn set_mtu(interface_name: &str, mtu: u16) -> Result<(), io::Error> {
));
}

// SAFETY: `interface_name.len()` is less than `ifr.ifr_name.len()`
unsafe {
std::ptr::copy_nonoverlapping(
interface_name.as_ptr() as *const libc::c_char,
Expand All @@ -36,6 +37,7 @@ pub fn set_mtu(interface_name: &str, mtu: u16) -> Result<(), io::Error> {
};
ifr.ifr_ifru.ifru_mtu = mtu as i32;

// SAFETY: SIOCSIFMTU expects an ifreq with an MTU and interface set
if unsafe { libc::ioctl(sock.as_raw_fd(), SIOCSIFMTU, &ifr) } < 0 {
let e = std::io::Error::last_os_error();
log::error!("{}", e.display_chain_with_msg("SIOCSIFMTU failed"));
Expand All @@ -59,6 +61,7 @@ pub fn get_mtu(interface_name: &str) -> Result<u16, io::Error> {
));
}

// SAFETY: `interface_name.len()` is less than `ifr.ifr_name.len()`
unsafe {
std::ptr::copy_nonoverlapping(
interface_name.as_ptr() as *const libc::c_char,
Expand All @@ -67,6 +70,7 @@ pub fn get_mtu(interface_name: &str) -> Result<u16, io::Error> {
)
};

// SAFETY: SIOCCIFMTU expects an ifreq with an interface set
if unsafe { libc::ioctl(sock.as_raw_fd(), SIOCGIFMTU, &ifr) } < 0 {
let e = std::io::Error::last_os_error();
log::error!("{}", e.display_chain_with_msg("SIOCGIFMTU failed"));
Expand Down

0 comments on commit bf2e1b7

Please sign in to comment.