Skip to content

Commit

Permalink
std::unix::os::home_dir: fallback's optimisation.
Browse files Browse the repository at this point in the history
we're using a guaranteed initialised field on success.
  • Loading branch information
devnexen committed Jun 23, 2024
1 parent d4cc01c commit bd9ce3e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions library/std/src/sys/pal/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,17 @@ pub fn home_dir() -> Option<PathBuf> {
n => n as usize,
};
let mut buf = Vec::with_capacity(amt);
let mut passwd: libc::passwd = mem::zeroed();
let mut p = mem::MaybeUninit::<libc::passwd>::uninit();
let mut result = ptr::null_mut();
match libc::getpwuid_r(
libc::getuid(),
&mut passwd,
p.as_mut_ptr(),
buf.as_mut_ptr(),
buf.capacity(),
&mut result,
) {
0 if !result.is_null() => {
let passwd = p.assume_init();
let ptr = passwd.pw_dir as *const _;
let bytes = CStr::from_ptr(ptr).to_bytes().to_vec();
Some(OsStringExt::from_vec(bytes))
Expand Down

0 comments on commit bd9ce3e

Please sign in to comment.