From 4bbaf5201b29d8d5f9f9917524020ad15ed88983 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 9 Mar 2024 15:26:08 -0800 Subject: [PATCH] Prevent future potential UB in unix wrapper for `getpwuid()` (#104) --- src/os/unix.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/os/unix.rs b/src/os/unix.rs index 3fdabf7..959e2ca 100644 --- a/src/os/unix.rs +++ b/src/os/unix.rs @@ -238,7 +238,6 @@ fn getpwuid(name: Name) -> Result { const BUF_SIZE: usize = 16_384; // size from the man page let mut buffer = mem::MaybeUninit::<[u8; BUF_SIZE]>::uninit(); let mut passwd = mem::MaybeUninit::::uninit(); - let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit(); // Get PassWd `struct`. let passwd = unsafe { @@ -251,6 +250,7 @@ fn getpwuid(name: Name) -> Result { target_os = "openbsd", ))] { + let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit(); let ret = getpwuid_r( geteuid(), passwd.as_mut_ptr(), @@ -268,6 +268,7 @@ fn getpwuid(name: Name) -> Result { if _passwd.is_null() { return Err(super::err_null_record()); } + passwd.assume_init() } #[cfg(target_os = "illumos")] @@ -282,9 +283,8 @@ fn getpwuid(name: Name) -> Result { if ret.is_null() { return Err(Error::last_os_error()); } + passwd.assume_init() } - - passwd.assume_init() }; // Extract names.