diff --git a/src/sockref.rs b/src/sockref.rs index f5807d80..257323be 100644 --- a/src/sockref.rs +++ b/src/sockref.rs @@ -109,8 +109,10 @@ where { /// The caller must ensure `S` is actually a socket. fn from(socket: &'s S) -> Self { + let fd = socket.as_raw_fd(); + assert!(fd >= 0); SockRef { - socket: ManuallyDrop::new(unsafe { Socket::from_raw_fd(socket.as_raw_fd()) }), + socket: ManuallyDrop::new(unsafe { Socket::from_raw_fd(fd) }), _lifetime: PhantomData, } } @@ -125,8 +127,10 @@ where { /// See the `From<&impl AsRawFd>` implementation. fn from(socket: &'s S) -> Self { + let socket = socket.as_raw_socket(); + assert!(socket != winapi::um::winsock2::INVALID_SOCKET as _); SockRef { - socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket.as_raw_socket()) }), + socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket) }), _lifetime: PhantomData, } }