Skip to content

Commit a9ec61d

Browse files
author
Joe Ellis
committed
Remove use of MaybeUninit in ucred.rs
We can simply init the struct directly. There is no real need to use uninit memory here.
1 parent ed20eff commit a9ec61d

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

library/std/src/sys/unix/ext/net.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ impl UnixStream {
433433
/// # Examples
434434
///
435435
/// ```no_run
436+
/// #![feature(peer_credentials_unix_socket)]
436437
/// use std::os::unix::net::UnixStream;
437438
///
438439
/// fn main() -> std::io::Result<()> {

library/std/src/sys/unix/ext/ucred.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub use self::impl_bsd::peer_cred;
3131
#[cfg(any(target_os = "linux", target_os = "android"))]
3232
pub mod impl_linux {
3333
use super::UCred;
34-
use crate::mem::MaybeUninit;
3534
use crate::os::unix::io::AsRawFd;
3635
use crate::os::unix::net::UnixStream;
3736
use crate::{io, mem};
@@ -46,9 +45,9 @@ pub mod impl_linux {
4645
assert!(ucred_size <= u32::max_value() as usize);
4746

4847
let mut ucred_size = ucred_size as u32;
48+
let mut ucred: ucred = ucred { pid: 1, uid: 1, gid: 1 };
4949

5050
unsafe {
51-
let mut ucred: ucred = MaybeUninit::uninit().assume_init();
5251
let ret = libc::getsockopt(
5352
socket.as_raw_fd(),
5453
libc::SOL_SOCKET,
@@ -76,14 +75,12 @@ pub mod impl_linux {
7675
pub mod impl_bsd {
7776
use super::UCred;
7877
use crate::io;
79-
use crate::mem::MaybeUninit;
8078
use crate::os::unix::io::AsRawFd;
8179
use crate::os::unix::net::UnixStream;
8280

8381
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
82+
let mut cred = UCred { uid: 1, gid: 1 };
8483
unsafe {
85-
// Create `cred` and attempt to populate it.
86-
let mut cred: UCred = MaybeUninit::uninit().assume_init();
8784
let ret = libc::getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
8885

8986
if ret == 0 { Ok(cred) } else { Err(io::Error::last_os_error()) }

0 commit comments

Comments
 (0)