Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not panic in OsIpcOneShotServer::new #351

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading