Skip to content

Commit f546573

Browse files
authored
Fix errors with char signedness. (#984)
When rustix is compiled with default-features = false, and linux-raw-sys is compiled with no_std, there is a difference in the signedness of `c_char`; fix rustix's code to handle this. Fixes #983.
1 parent 7552d81 commit f546573

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/backend/linux_raw/net/netdevice.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
#![allow(unsafe_code)]
22

3-
#[cfg(feature = "alloc")]
4-
use crate::alloc::string::String;
53
use crate::backend::io::syscalls::ioctl;
64
use crate::fd::AsFd;
75
use crate::io;
6+
use core::slice;
7+
use core::str;
8+
use linux_raw_sys::ctypes::c_char;
89
use linux_raw_sys::ioctl::SIOCGIFINDEX;
910
#[cfg(feature = "alloc")]
1011
use linux_raw_sys::ioctl::SIOCGIFNAME;
1112
use linux_raw_sys::net::{ifreq, ifreq__bindgen_ty_1, ifreq__bindgen_ty_2, IFNAMSIZ};
13+
#[cfg(feature = "alloc")]
14+
use {alloc::borrow::ToOwned, alloc::string::String};
1215

1316
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
1417
let if_name_bytes = if_name.as_bytes();
1518
if if_name_bytes.len() >= IFNAMSIZ as usize {
1619
return Err(io::Errno::NODEV);
1720
}
21+
if if_name_bytes.contains(&0) {
22+
return Err(io::Errno::NODEV);
23+
}
24+
25+
// SAFETY: Convert `&[u8]` to `&[c_char]`.
26+
let if_name_bytes = unsafe {
27+
slice::from_raw_parts(if_name_bytes.as_ptr().cast::<c_char>(), if_name_bytes.len())
28+
};
1829

1930
let mut ifreq = ifreq {
2031
ifr_ifrn: ifreq__bindgen_ty_1 { ifrn_name: [0; 16] },
@@ -40,14 +51,17 @@ pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
4051

4152
if let Some(nul_byte) = unsafe { ifreq.ifr_ifrn.ifrn_name }
4253
.iter()
43-
.position(|char| *char == 0)
54+
.position(|ch| *ch == 0)
4455
{
45-
let name = unsafe { ifreq.ifr_ifrn.ifrn_name }[..nul_byte]
46-
.iter()
47-
.map(|v| *v as char)
48-
.collect();
56+
let ifrn_name = unsafe { &ifreq.ifr_ifrn.ifrn_name[..nul_byte] };
57+
58+
// SAFETY: Convert `&[c_char]` to `&[u8]`.
59+
let ifrn_name =
60+
unsafe { slice::from_raw_parts(ifrn_name.as_ptr().cast::<u8>(), ifrn_name.len()) };
4961

50-
Ok(name)
62+
str::from_utf8(ifrn_name)
63+
.map_err(|_| io::Errno::ILSEQ)
64+
.map(ToOwned::to_owned)
5165
} else {
5266
Err(io::Errno::INVAL)
5367
}

0 commit comments

Comments
 (0)