Skip to content

Commit

Permalink
Do not panic in OsIpcOneShotServer::new (#351)
Browse files Browse the repository at this point in the history
Co-authored-by: xmakro <makro@>
  • Loading branch information
xmakro authored Aug 2, 2024
1 parent 24eee8a commit 6170906
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/platform/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ mod fragment_tests {
use lazy_static::lazy_static;

lazy_static! {
static ref FRAGMENT_SIZE: usize = { platform::OsIpcSender::get_max_fragment_size() };
static ref FRAGMENT_SIZE: usize = platform::OsIpcSender::get_max_fragment_size();
}

#[test]
Expand Down
19 changes: 12 additions & 7 deletions src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ impl OsIpcSender {
// using a reduced send buffer size.
//
// Any other errors we might get here are non-recoverable.
if !(error == UnixError::Errno(libc::ENOBUFS)
if !(matches!(error, UnixError::Errno(libc::ENOBUFS))
&& downsize(&mut sendbuf_size, data.len()).is_ok())
{
return Err(error);
Expand Down Expand Up @@ -431,7 +431,7 @@ impl OsIpcSender {
};

if let Err(error) = result {
if error == UnixError::Errno(libc::ENOBUFS)
if matches!(error, UnixError::Errno(libc::ENOBUFS))
&& downsize(&mut sendbuf_size, end_byte_position - byte_position).is_ok()
{
// If the kernel failed to allocate a buffer large enough for the packet,
Expand Down Expand Up @@ -673,7 +673,7 @@ impl OsIpcOneShotServer {
pub fn new() -> Result<(OsIpcOneShotServer, String), UnixError> {
unsafe {
let fd = libc::socket(libc::AF_UNIX, SOCK_SEQPACKET | SOCK_FLAGS, 0);
let temp_dir = Builder::new().tempdir().unwrap();
let temp_dir = Builder::new().tempdir()?;
let socket_path = temp_dir.path().join("socket");
let path_string = socket_path.to_str().unwrap();

Expand Down Expand Up @@ -902,10 +902,11 @@ impl OsIpcSharedMemory {
}
}

#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Debug)]
pub enum UnixError {
Errno(c_int),
ChannelClosed,
IoError(io::Error),
}

impl UnixError {
Expand All @@ -915,15 +916,18 @@ impl UnixError {

#[allow(dead_code)]
pub fn channel_is_closed(&self) -> bool {
*self == UnixError::ChannelClosed
matches!(self, UnixError::ChannelClosed)
}
}

impl fmt::Display for UnixError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
UnixError::Errno(errno) => fmt::Display::fmt(&io::Error::from_raw_os_error(errno), fmt),
match self {
UnixError::Errno(errno) => {
fmt::Display::fmt(&io::Error::from_raw_os_error(*errno), fmt)
},
UnixError::ChannelClosed => write!(fmt, "All senders for this socket closed"),
UnixError::IoError(e) => write!(fmt, "{e}"),
}
}
}
Expand All @@ -941,6 +945,7 @@ impl From<UnixError> for io::Error {
match unix_error {
UnixError::Errno(errno) => io::Error::from_raw_os_error(errno),
UnixError::ChannelClosed => io::Error::new(io::ErrorKind::ConnectionReset, unix_error),
UnixError::IoError(e) => e,
}
}
}
Expand Down

0 comments on commit 6170906

Please sign in to comment.