summaryrefslogtreecommitdiff
path: root/src/sys/socket
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-09-04 14:47:40 +0000
committerGitHub <noreply@github.com>2019-09-04 14:47:40 +0000
commit54cd6fe77977652256d695d565e1b1a5fe4d5613 (patch)
tree7b9854e489e377e19df86e696a79c48343a2647f /src/sys/socket
parenta4a465d25567f163f9552b977eb4d17435251d41 (diff)
parent43a71ff5e385a9af455b275736b520184f7fa00c (diff)
downloadnix-54cd6fe77977652256d695d565e1b1a5fe4d5613.zip
Merge #1114
1114: Replace most instances of mem::uninitialized with mem::MaybeUninit r=asomers a=asomers Only two instances remain: * For the deprecated sys::socket::CmsgSpace::new. We should probably just remove that method. * For sys::termios::Termios::default_uninit. This will require some more thought. Co-authored-by: Alan Somers <asomers@gmail.com>
Diffstat (limited to 'src/sys/socket')
-rw-r--r--src/sys/socket/addr.rs4
-rw-r--r--src/sys/socket/mod.rs60
2 files changed, 33 insertions, 31 deletions
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs
index cbec71f7..2ad453f7 100644
--- a/src/sys/socket/addr.rs
+++ b/src/sys/socket/addr.rs
@@ -475,9 +475,7 @@ impl Ipv6Addr {
#[allow(clippy::many_single_char_names)]
#[allow(clippy::too_many_arguments)]
pub fn new(a: u16, b: u16, c: u16, d: u16, e: u16, f: u16, g: u16, h: u16) -> Ipv6Addr {
- let mut in6_addr_var: libc::in6_addr = unsafe{mem::uninitialized()};
- in6_addr_var.s6_addr = to_u8_array!(a,b,c,d,e,f,g,h);
- Ipv6Addr(in6_addr_var)
+ Ipv6Addr(libc::in6_addr{s6_addr: to_u8_array!(a,b,c,d,e,f,g,h)})
}
pub fn from_std(std: &net::Ipv6Addr) -> Ipv6Addr {
diff --git a/src/sys/socket/mod.rs b/src/sys/socket/mod.rs
index 662d8803..beaa69ad 100644
--- a/src/sys/socket/mod.rs
+++ b/src/sys/socket/mod.rs
@@ -854,20 +854,21 @@ pub fn sendmsg(fd: RawFd, iov: &[IoVec<&[u8]>], cmsgs: &[ControlMessage],
ptr::null_mut()
};
- let mhdr = {
+ let mhdr = unsafe {
// Musl's msghdr has private fields, so this is the only way to
// initialize it.
- let mut mhdr: msghdr = unsafe{mem::uninitialized()};
- mhdr.msg_name = name as *mut _;
- mhdr.msg_namelen = namelen;
+ let mut mhdr = mem::MaybeUninit::<msghdr>::uninit();
+ let p = mhdr.as_mut_ptr();
+ (*p).msg_name = name as *mut _;
+ (*p).msg_namelen = namelen;
// transmute iov into a mutable pointer. sendmsg doesn't really mutate
// the buffer, but the standard says that it takes a mutable pointer
- mhdr.msg_iov = iov.as_ptr() as *mut _;
- mhdr.msg_iovlen = iov.len() as _;
- mhdr.msg_control = cmsg_ptr;
- mhdr.msg_controllen = capacity as _;
- mhdr.msg_flags = 0;
- mhdr
+ (*p).msg_iov = iov.as_ptr() as *mut _;
+ (*p).msg_iovlen = iov.len() as _;
+ (*p).msg_control = cmsg_ptr;
+ (*p).msg_controllen = capacity as _;
+ (*p).msg_flags = 0;
+ mhdr.assume_init()
};
// Encode each cmsg. This must happen after initializing the header because
@@ -898,7 +899,7 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
cmsg_buffer: Option<&'a mut dyn CmsgBuffer>,
flags: MsgFlags) -> Result<RecvMsg<'a>>
{
- let mut address: sockaddr_storage = unsafe { mem::uninitialized() };
+ let mut address = mem::MaybeUninit::uninit();
let (msg_control, msg_controllen) = match cmsg_buffer {
Some(cmsgspace) => {
let msg_buf = cmsgspace.as_bytes_mut();
@@ -906,18 +907,19 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
},
None => (ptr::null_mut(), 0),
};
- let mut mhdr = {
+ let mut mhdr = unsafe {
// Musl's msghdr has private fields, so this is the only way to
// initialize it.
- let mut mhdr: msghdr = unsafe{mem::uninitialized()};
- mhdr.msg_name = &mut address as *mut sockaddr_storage as *mut c_void;
- mhdr.msg_namelen = mem::size_of::<sockaddr_storage>() as socklen_t;
- mhdr.msg_iov = iov.as_ptr() as *mut iovec;
- mhdr.msg_iovlen = iov.len() as _;
- mhdr.msg_control = msg_control as *mut c_void;
- mhdr.msg_controllen = msg_controllen as _;
- mhdr.msg_flags = 0;
- mhdr
+ let mut mhdr = mem::MaybeUninit::<msghdr>::uninit();
+ let p = mhdr.as_mut_ptr();
+ (*p).msg_name = address.as_mut_ptr() as *mut c_void;
+ (*p).msg_namelen = mem::size_of::<sockaddr_storage>() as socklen_t;
+ (*p).msg_iov = iov.as_ptr() as *mut iovec;
+ (*p).msg_iovlen = iov.len() as _;
+ (*p).msg_control = msg_control as *mut c_void;
+ (*p).msg_controllen = msg_controllen as _;
+ (*p).msg_flags = 0;
+ mhdr.assume_init()
};
let ret = unsafe { libc::recvmsg(fd, &mut mhdr, flags.bits()) };
@@ -935,7 +937,9 @@ pub fn recvmsg<'a>(fd: RawFd, iov: &[IoVec<&mut [u8]>],
};
let address = unsafe {
- sockaddr_storage_to_addr(&address, mhdr.msg_namelen as usize).ok()
+ sockaddr_storage_to_addr(&address.assume_init(),
+ mhdr.msg_namelen as usize
+ ).ok()
};
RecvMsg {
bytes: r as usize,
@@ -1190,18 +1194,18 @@ pub fn setsockopt<O: SetSockOpt>(fd: RawFd, opt: O, val: &O::Val) -> Result<()>
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html)
pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
unsafe {
- let mut addr: sockaddr_storage = mem::uninitialized();
+ let mut addr = mem::MaybeUninit::uninit();
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
let ret = libc::getpeername(
fd,
- &mut addr as *mut libc::sockaddr_storage as *mut libc::sockaddr,
+ addr.as_mut_ptr() as *mut libc::sockaddr,
&mut len
);
Errno::result(ret)?;
- sockaddr_storage_to_addr(&addr, len as usize)
+ sockaddr_storage_to_addr(&addr.assume_init(), len as usize)
}
}
@@ -1210,18 +1214,18 @@ pub fn getpeername(fd: RawFd) -> Result<SockAddr> {
/// [Further reading](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html)
pub fn getsockname(fd: RawFd) -> Result<SockAddr> {
unsafe {
- let mut addr: sockaddr_storage = mem::uninitialized();
+ let mut addr = mem::MaybeUninit::uninit();
let mut len = mem::size_of::<sockaddr_storage>() as socklen_t;
let ret = libc::getsockname(
fd,
- &mut addr as *mut libc::sockaddr_storage as *mut libc::sockaddr,
+ addr.as_mut_ptr() as *mut libc::sockaddr,
&mut len
);
Errno::result(ret)?;
- sockaddr_storage_to_addr(&addr, len as usize)
+ sockaddr_storage_to_addr(&addr.assume_init(), len as usize)
}
}