Skip to content

Commit

Permalink
Fix potential fd leak in new_socket on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Nov 30, 2022
1 parent 4cd722d commit edff846
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/sys/windows/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::net::SocketAddr;
use std::sync::Once;

use windows_sys::Win32::Networking::WinSock::{
ioctlsocket, socket, AF_INET, AF_INET6, FIONBIO, IN6_ADDR, IN6_ADDR_0, INVALID_SOCKET, IN_ADDR,
IN_ADDR_0, SOCKADDR, SOCKADDR_IN, SOCKADDR_IN6, SOCKADDR_IN6_0, SOCKET,
closesocket, ioctlsocket, socket, AF_INET, AF_INET6, FIONBIO, IN6_ADDR, IN6_ADDR_0,
INVALID_SOCKET, IN_ADDR, IN_ADDR_0, SOCKADDR, SOCKADDR_IN, SOCKADDR_IN6, SOCKADDR_IN6_0,
SOCKET,
};

/// Initialise the network stack for Windows.
Expand All @@ -32,14 +33,18 @@ pub(crate) fn new_ip_socket(addr: SocketAddr, socket_type: u16) -> io::Result<SO
pub(crate) fn new_socket(domain: u32, socket_type: u16) -> io::Result<SOCKET> {
init();

syscall!(
let socket = syscall!(
socket(domain as i32, socket_type as i32, 0),
PartialEq::eq,
INVALID_SOCKET
)
.and_then(|socket| {
syscall!(ioctlsocket(socket, FIONBIO, &mut 1), PartialEq::ne, 0).map(|_| socket as SOCKET)
})
)?;

if let Err(err) = syscall!(ioctlsocket(socket, FIONBIO, &mut 1), PartialEq::ne, 0) {
let _ = unsafe { closesocket(socket) };
return Err(err);
}

Ok(socket as SOCKET)
}

/// A type with the same memory layout as `SOCKADDR`. Used in converting Rust level
Expand Down

0 comments on commit edff846

Please sign in to comment.