11#![ allow( unsafe_code) ]
22
3- #[ cfg( feature = "alloc" ) ]
4- use crate :: alloc:: string:: String ;
53use crate :: backend:: io:: syscalls:: ioctl;
64use crate :: fd:: AsFd ;
75use crate :: io;
6+ use core:: slice;
7+ use core:: str;
8+ use linux_raw_sys:: ctypes:: c_char;
89use linux_raw_sys:: ioctl:: SIOCGIFINDEX ;
910#[ cfg( feature = "alloc" ) ]
1011use linux_raw_sys:: ioctl:: SIOCGIFNAME ;
1112use 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
1316pub ( 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