Use unsafe blocks in sys/socket.h implementation
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
//!
|
||||
//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
|
||||
|
||||
// TODO: set this for entire crate when possible
|
||||
#![deny(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use core::{mem, ptr};
|
||||
|
||||
use crate::{
|
||||
@@ -119,7 +122,7 @@ pub unsafe extern "C" fn CMSG_DATA(cmsg: *const cmsghdr) -> *mut c_uchar {
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn CMSG_NXTHDR(mhdr: *const msghdr, cmsg: *const cmsghdr) -> *mut cmsghdr {
|
||||
if cmsg.is_null() {
|
||||
return CMSG_FIRSTHDR(mhdr);
|
||||
return unsafe { CMSG_FIRSTHDR(mhdr) };
|
||||
};
|
||||
|
||||
unsafe {
|
||||
@@ -155,13 +158,14 @@ pub unsafe extern "C" fn CMSG_ALIGN(len: size_t) -> size_t {
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn CMSG_SPACE(len: c_uint) -> c_uint {
|
||||
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(mem::size_of::<cmsghdr>())) as c_uint
|
||||
(unsafe { CMSG_ALIGN(len as size_t) } + unsafe { CMSG_ALIGN(mem::size_of::<cmsghdr>()) })
|
||||
as c_uint
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_socket.h.html>.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn CMSG_LEN(length: c_uint) -> c_uint {
|
||||
(CMSG_ALIGN(mem::size_of::<cmsghdr>()) + length as usize) as c_uint
|
||||
(unsafe { CMSG_ALIGN(mem::size_of::<cmsghdr>()) } + length as usize) as c_uint
|
||||
}
|
||||
// } These must match C macros in include/bits/sys/socket.h
|
||||
|
||||
@@ -173,7 +177,7 @@ pub unsafe extern "C" fn accept(
|
||||
address_len: *mut socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::accept(socket, address, address_len).or_minus_one_errno(),
|
||||
unsafe { Sys::accept(socket, address, address_len) }.or_minus_one_errno(),
|
||||
"accept({}, {:p}, {:p})",
|
||||
socket,
|
||||
address,
|
||||
@@ -189,7 +193,7 @@ pub unsafe extern "C" fn bind(
|
||||
address_len: socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::bind(socket, address, address_len)
|
||||
unsafe { Sys::bind(socket, address, address_len) }
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"bind({}, {:p}, {})",
|
||||
@@ -207,7 +211,7 @@ pub unsafe extern "C" fn connect(
|
||||
address_len: socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::connect(socket, address, address_len).or_minus_one_errno(),
|
||||
unsafe { Sys::connect(socket, address, address_len) }.or_minus_one_errno(),
|
||||
"connect({}, {:p}, {})",
|
||||
socket,
|
||||
address,
|
||||
@@ -223,7 +227,7 @@ pub unsafe extern "C" fn getpeername(
|
||||
address_len: *mut socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::getpeername(socket, address, address_len)
|
||||
unsafe { Sys::getpeername(socket, address, address_len) }
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"getpeername({}, {:p}, {:p})",
|
||||
@@ -241,7 +245,7 @@ pub unsafe extern "C" fn getsockname(
|
||||
address_len: *mut socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::getsockname(socket, address, address_len)
|
||||
unsafe { Sys::getsockname(socket, address, address_len) }
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"getsockname({}, {:p}, {:p})",
|
||||
@@ -261,7 +265,7 @@ pub unsafe extern "C" fn getsockopt(
|
||||
option_len: *mut socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::getsockopt(socket, level, option_name, option_value, option_len)
|
||||
unsafe { Sys::getsockopt(socket, level, option_name, option_value, option_len) }
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"getsockopt({}, {}, {}, {:p}, {:p})",
|
||||
@@ -289,14 +293,16 @@ pub unsafe extern "C" fn recv(
|
||||
length: size_t,
|
||||
flags: c_int,
|
||||
) -> ssize_t {
|
||||
recvfrom(
|
||||
socket,
|
||||
buffer,
|
||||
length,
|
||||
flags,
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
unsafe {
|
||||
recvfrom(
|
||||
socket,
|
||||
buffer,
|
||||
length,
|
||||
flags,
|
||||
ptr::null_mut(),
|
||||
ptr::null_mut(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/recvfrom.html>.
|
||||
@@ -310,7 +316,7 @@ pub unsafe extern "C" fn recvfrom(
|
||||
address_len: *mut socklen_t,
|
||||
) -> ssize_t {
|
||||
trace_expr!(
|
||||
Sys::recvfrom(socket, buffer, length, flags, address, address_len)
|
||||
unsafe { Sys::recvfrom(socket, buffer, length, flags, address, address_len) }
|
||||
.map(|r| r as ssize_t)
|
||||
.or_minus_one_errno(),
|
||||
"recvfrom({}, {:p}, {}, {:#x}, {:p}, {:p})",
|
||||
@@ -326,7 +332,7 @@ pub unsafe extern "C" fn recvfrom(
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/recvmsg.html>.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn recvmsg(socket: c_int, msg: *mut msghdr, flags: c_int) -> ssize_t {
|
||||
Sys::recvmsg(socket, msg, flags)
|
||||
unsafe { Sys::recvmsg(socket, msg, flags) }
|
||||
.map(|r| r as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
@@ -339,13 +345,13 @@ pub unsafe extern "C" fn send(
|
||||
length: size_t,
|
||||
flags: c_int,
|
||||
) -> ssize_t {
|
||||
sendto(socket, message, length, flags, ptr::null(), 0)
|
||||
unsafe { sendto(socket, message, length, flags, ptr::null(), 0) }
|
||||
}
|
||||
|
||||
/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/sendmsg.html>.
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn sendmsg(socket: c_int, msg: *const msghdr, flags: c_int) -> ssize_t {
|
||||
Sys::sendmsg(socket, msg, flags)
|
||||
unsafe { Sys::sendmsg(socket, msg, flags) }
|
||||
.map(|w| w as ssize_t)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
@@ -361,7 +367,7 @@ pub unsafe extern "C" fn sendto(
|
||||
dest_len: socklen_t,
|
||||
) -> ssize_t {
|
||||
trace_expr!(
|
||||
Sys::sendto(socket, message, length, flags, dest_addr, dest_len)
|
||||
unsafe { Sys::sendto(socket, message, length, flags, dest_addr, dest_len) }
|
||||
.map(|w| w as ssize_t)
|
||||
.or_minus_one_errno(),
|
||||
"sendto({}, {:p}, {}, {:#x}, {:p}, {})",
|
||||
@@ -384,7 +390,7 @@ pub unsafe extern "C" fn setsockopt(
|
||||
option_len: socklen_t,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::setsockopt(socket, level, option_name, option_value, option_len)
|
||||
unsafe { Sys::setsockopt(socket, level, option_name, option_value, option_len) }
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"setsockopt({}, {}, {}, {:p}, {})",
|
||||
@@ -406,7 +412,7 @@ pub unsafe extern "C" fn shutdown(socket: c_int, how: c_int) -> c_int {
|
||||
#[unsafe(no_mangle)]
|
||||
pub unsafe extern "C" fn socket(domain: c_int, kind: c_int, protocol: c_int) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::socket(domain, kind, protocol).or_minus_one_errno(),
|
||||
unsafe { Sys::socket(domain, kind, protocol) }.or_minus_one_errno(),
|
||||
"socket({}, {}, {})",
|
||||
domain,
|
||||
kind,
|
||||
@@ -423,9 +429,11 @@ pub unsafe extern "C" fn socketpair(
|
||||
sv: *mut c_int,
|
||||
) -> c_int {
|
||||
trace_expr!(
|
||||
Sys::socketpair(domain, kind, protocol, &mut *(sv as *mut [c_int; 2]))
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
Sys::socketpair(domain, kind, protocol, unsafe {
|
||||
&mut *(sv as *mut [c_int; 2])
|
||||
})
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno(),
|
||||
"socketpair({}, {}, {}, {:p})",
|
||||
domain,
|
||||
kind,
|
||||
|
||||
Reference in New Issue
Block a user