From bd9ce3e0747a2bba3a322703c87c1d44d85deadd Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 23 Jun 2024 08:22:51 +0100 Subject: [PATCH] std::unix::os::home_dir: fallback's optimisation. we're using a guaranteed initialised field on success. --- library/std/src/sys/pal/unix/os.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index 2e71ceceb58..3e690ce60d2 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -738,16 +738,17 @@ unsafe fn fallback() -> Option { n => n as usize, }; let mut buf = Vec::with_capacity(amt); - let mut passwd: libc::passwd = mem::zeroed(); + let mut p = mem::MaybeUninit::::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))