Skip to content
Open
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
51 changes: 39 additions & 12 deletions varlink/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ impl Listener {
}
}

pub const fn is_already_accepted(&self) -> bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I know the rest of the code is not consistent about this, having at least a minimal documentation string I think is a good idea for public APIs.

match *self {
Self::TCP(_, value) => value,
Self::UNIX(_, value) => value,
}
}

#[cfg(windows)]
pub fn accept(&self, timeout: u64) -> Result<Box<dyn Stream>> {
use winapi::um::winsock2::WSAEINTR as EINTR;
Expand Down Expand Up @@ -194,13 +201,21 @@ impl Listener {
}

match self {
&Listener::TCP(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
&Listener::TCP(Some(ref l), accepted) => {
if accepted {
unsafe { Ok(Box::new(TcpStream::from_raw_fd(l.as_raw_fd()))) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without digging into this code a lot more, there's a reason those functions are unsafe and that reason is exactly what looks like a bug here: The previous listener still thinks it owns the file descriptor.

We can avoid this by deconstructing into an OwnedFd like Box::new(TcpStream::from(l.into::<OwnedFd>())) or so.

But I think we need to back up on this, I'll comment on the original ticket.

} else {
Comment on lines +205 to +207
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using from_raw_fd() transfers ownership of the file descriptor without validation. This could lead to use-after-free or double-close issues if the original listener is used elsewhere. Consider using a safer approach like cloning the file descriptor with dup() first.

Copilot uses AI. Check for mistakes.
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
Listener::UNIX(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
Listener::UNIX(Some(ref l), accepted) => {
if *accepted {
unsafe { Ok(Box::new(UnixStream::from_raw_fd(l.as_raw_fd()))) }
Comment on lines +213 to +214
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent dereferencing pattern: TCP case uses accepted directly while UNIX case uses *accepted. Both should use the same pattern for consistency.

Copilot uses AI. Check for mistakes.
} else {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
_ => Err(context!(ErrorKind::ConnectionClosed)),
}
Expand Down Expand Up @@ -250,13 +265,21 @@ impl Listener {
}
}
match self {
&Listener::TCP(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
&Listener::TCP(Some(ref l), accepted) => {
if accepted {
unsafe { Ok(Box::new(TcpStream::from_raw_fd(l.as_raw_fd()))) }
} else {
Comment on lines +269 to +271
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is duplicated from the Windows version above (lines 205-210). Consider extracting this logic into a helper method to reduce code duplication.

Copilot uses AI. Check for mistakes.
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
Listener::UNIX(Some(ref l), _) => {
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
Listener::UNIX(Some(ref l), accepted) => {
if *accepted {
unsafe { Ok(Box::new(UnixStream::from_raw_fd(l.as_raw_fd()))) }
} else {
Comment on lines +277 to +279
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is duplicated from both the Windows version and the TCP case above. The same dereferencing inconsistency and duplication issues apply here.

Copilot uses AI. Check for mistakes.
let (s, _addr) = l.accept().map_err(map_context!())?;
Ok(Box::new(s))
}
}
_ => Err(context!(ErrorKind::ConnectionClosed)),
}
Expand Down Expand Up @@ -607,5 +630,9 @@ pub fn listen<S: ?Sized + AsRef<str>, H: crate::ConnectionHandler + Send + Sync
}
}
});

if listener.is_already_accepted() {
return Ok(());
}
}
}