Skip to content

Commit

Permalink
Prevent future potential UB in unix wrapper for getpwuid() (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz authored and AldaronLau committed Mar 10, 2024
1 parent bc0c66d commit 9e7a5c8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,6 @@ fn getpwuid(name: Name) -> Result<OsString> {
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::<PassWd>::uninit();
let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit();

// Get PassWd `struct`.
let passwd = unsafe {
Expand All @@ -249,6 +248,7 @@ fn getpwuid(name: Name) -> Result<OsString> {
target_os = "openbsd",
))]
{
let mut _passwd = mem::MaybeUninit::<*mut PassWd>::uninit();
let ret = getpwuid_r(
geteuid(),
passwd.as_mut_ptr(),
Expand All @@ -266,6 +266,7 @@ fn getpwuid(name: Name) -> Result<OsString> {
if _passwd.is_null() {
return Err(super::err_null_record());
}
passwd.assume_init()
}

#[cfg(target_os = "illumos")]
Expand All @@ -280,9 +281,8 @@ fn getpwuid(name: Name) -> Result<OsString> {
if ret.is_null() {
return Err(Error::last_os_error());
}
passwd.assume_init()
}

passwd.assume_init()
};

// Extract names.
Expand Down

0 comments on commit 9e7a5c8

Please sign in to comment.