From eb265d0841e64ba4eb67c2107eec235ec6e5c7dd Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 23 Jun 2024 08:22:51 +0100 Subject: [PATCH 1/2] std::unix::os::home_dir: fallback's optimisation. we're using a guaranteed initialised field on success. --- std/src/sys/pal/unix/os.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/std/src/sys/pal/unix/os.rs b/std/src/sys/pal/unix/os.rs index 2e71ceceb58b1..3e690ce60d2b2 100644 --- a/std/src/sys/pal/unix/os.rs +++ b/std/src/sys/pal/unix/os.rs @@ -738,16 +738,17 @@ pub fn home_dir() -> 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)) From f0b95fcce7175212ab50de660cafedc42bad0cc8 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 23 Jun 2024 09:56:02 +0100 Subject: [PATCH 2/2] fix build --- std/src/sys/pal/unix/os.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/std/src/sys/pal/unix/os.rs b/std/src/sys/pal/unix/os.rs index 3e690ce60d2b2..40e2d1403ef5e 100644 --- a/std/src/sys/pal/unix/os.rs +++ b/std/src/sys/pal/unix/os.rs @@ -748,8 +748,7 @@ pub fn home_dir() -> Option { &mut result, ) { 0 if !result.is_null() => { - let passwd = p.assume_init(); - let ptr = passwd.pw_dir as *const _; + let ptr = (*result).pw_dir as *const _; let bytes = CStr::from_ptr(ptr).to_bytes().to_vec(); Some(OsStringExt::from_vec(bytes)) }