Skip to content

Commit 3057398

Browse files
committed
changes from feedback
1 parent dab4104 commit 3057398

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

library/std/src/os/unix/net/datagram.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use libc::MSG_NOSIGNAL;
1616
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
1717
use super::{sockaddr_un, SocketAddr};
1818
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
19-
use crate::ffi::CStr;
19+
use crate::ffi::{CStr, CString};
2020
#[cfg(any(doc, target_os = "android", target_os = "linux"))]
2121
use crate::io::{IoSlice, IoSliceMut};
2222
use crate::net::Shutdown;
@@ -886,7 +886,7 @@ impl UnixDatagram {
886886
/// ```
887887
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
888888
#[unstable(feature = "unix_set_todevice", issue = "129182")]
889-
pub fn todevice(&self) -> io::Result<&CStr> {
889+
pub fn todevice(&self) -> io::Result<CString> {
890890
self.0.todevice()
891891
}
892892
/// Returns the value of the `SO_ERROR` option.

library/std/src/os/unix/net/stream.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::{peer_cred, UCred};
1313
use super::{recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to, SocketAncillary};
1414
use super::{sockaddr_un, SocketAddr};
1515
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
16-
use crate::ffi::CStr;
16+
use crate::ffi::{CStr, CString};
1717
use crate::fmt;
1818
use crate::io::{self, IoSlice, IoSliceMut};
1919
use crate::net::Shutdown;
@@ -455,7 +455,7 @@ impl UnixStream {
455455
/// ```
456456
#[cfg(any(doc, target_os = "linux", target_os = "haiku", target_os = "vxworks",))]
457457
#[unstable(feature = "unix_set_todevice", issue = "129182")]
458-
pub fn todevice(&self) -> io::Result<&CStr> {
458+
pub fn todevice(&self) -> io::Result<CString> {
459459
self.0.todevice()
460460
}
461461

library/std/src/sys/pal/unix/net.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use libc::{c_int, c_void, size_t, sockaddr, socklen_t, MSG_PEEK};
22

3-
use crate::ffi::CStr;
3+
use crate::ffi::{CStr, CString};
44
use crate::io::{self, BorrowedBuf, BorrowedCursor, IoSlice, IoSliceMut};
55
use crate::net::{Shutdown, SocketAddr};
66
use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
@@ -564,18 +564,24 @@ impl Socket {
564564
}
565565

566566
#[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))]
567-
pub fn todevice(&self) -> io::Result<&CStr> {
567+
pub fn todevice(&self) -> io::Result<CString> {
568568
let buf: [libc::c_char; libc::IFNAMSIZ] =
569569
getsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE)?;
570570
let s: &[u8] = unsafe { core::slice::from_raw_parts(buf.as_ptr() as *const u8, buf.len()) };
571-
let ifrname = CStr::from_bytes_until_nul(s).unwrap();
572-
Ok(ifrname)
571+
let name = CStr::from_bytes_until_nul(s).unwrap();
572+
Ok(CString::new(name.to_bytes()).unwrap())
573573
}
574574

575575
#[cfg(any(target_os = "linux", target_os = "haiku", target_os = "vxworks"))]
576576
pub fn set_todevice(&self, ifrname: &CStr) -> io::Result<()> {
577+
let istr = ifrname.to_bytes();
578+
579+
if istr.len() >= libc::IFNAMSIZ {
580+
return Err(io::Error::from_raw_os_error(libc::ENAMETOOLONG));
581+
}
582+
577583
let mut buf = [0; libc::IFNAMSIZ];
578-
for (src, dst) in ifrname.to_bytes().iter().zip(&mut buf[..libc::IFNAMSIZ - 1]) {
584+
for (src, dst) in istr.iter().zip(&mut buf[..libc::IFNAMSIZ - 1]) {
579585
*dst = *src as libc::c_char;
580586
}
581587
setsockopt(self, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, buf)

0 commit comments

Comments
 (0)