Skip to content

Make *::from_anything methods unsafe #1979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 46 additions & 16 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3631,7 +3631,12 @@ impl From<User> for libc::passwd {

#[cfg(not(target_os = "redox"))] // RedoxFS does not support passwd
impl User {
fn from_anything<F>(f: F) -> Result<Option<Self>>
/// # Safety
///
/// If `f` writes to its `*mut *mut libc::passwd` parameter, then it must
/// also initialize the value pointed to by its `*mut libc::group`
/// parameter.
unsafe fn from_anything<F>(f: F) -> Result<Option<Self>>
where
F: Fn(
*mut libc::passwd,
Expand Down Expand Up @@ -3661,7 +3666,9 @@ impl User {
if res.is_null() {
return Ok(None);
} else {
let pwd = unsafe { pwd.assume_init() };
// SAFETY: `f` guarantees that `pwd` is initialized if `res`
// is not null.
let pwd = pwd.assume_init();
return Ok(Some(User::from(&pwd)));
}
} else if Errno::last() == Errno::ERANGE {
Expand All @@ -3687,9 +3694,13 @@ impl User {
/// assert_eq!(res.name, "root");
/// ```
pub fn from_uid(uid: Uid) -> Result<Option<Self>> {
User::from_anything(|pwd, cbuf, cap, res| unsafe {
libc::getpwuid_r(uid.0, pwd, cbuf, cap, res)
})
// SAFETY: `getpwuid_r` will write to `res` if it initializes the value
// at `pwd`.
unsafe {
User::from_anything(|pwd, cbuf, cap, res| {
libc::getpwuid_r(uid.0, pwd, cbuf, cap, res)
})
}
}

/// Get a user by name.
Expand All @@ -3710,9 +3721,13 @@ impl User {
Ok(c_str) => c_str,
Err(_nul_error) => return Ok(None),
};
User::from_anything(|pwd, cbuf, cap, res| unsafe {
libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res)
})
// SAFETY: `getpwnam_r` will write to `res` if it initializes the value
// at `pwd`.
unsafe {
User::from_anything(|pwd, cbuf, cap, res| {
libc::getpwnam_r(name.as_ptr(), pwd, cbuf, cap, res)
})
}
}
}

Expand Down Expand Up @@ -3763,7 +3778,12 @@ impl Group {
ret
}

fn from_anything<F>(f: F) -> Result<Option<Self>>
/// # Safety
///
/// If `f` writes to its `*mut *mut libc::group` parameter, then it must
/// also initialize the value pointed to by its `*mut libc::group`
/// parameter.
unsafe fn from_anything<F>(f: F) -> Result<Option<Self>>
where
F: Fn(
*mut libc::group,
Expand Down Expand Up @@ -3793,7 +3813,9 @@ impl Group {
if res.is_null() {
return Ok(None);
} else {
let grp = unsafe { grp.assume_init() };
// SAFETY: `f` guarantees that `grp` is initialized if `res`
// is not null.
let grp = grp.assume_init();
return Ok(Some(Group::from(&grp)));
}
} else if Errno::last() == Errno::ERANGE {
Expand Down Expand Up @@ -3821,9 +3843,13 @@ impl Group {
/// assert!(res.name == "root");
/// ```
pub fn from_gid(gid: Gid) -> Result<Option<Self>> {
Group::from_anything(|grp, cbuf, cap, res| unsafe {
libc::getgrgid_r(gid.0, grp, cbuf, cap, res)
})
// SAFETY: `getgrgid_r` will write to `res` if it initializes the value
// at `grp`.
unsafe {
Group::from_anything(|grp, cbuf, cap, res| {
libc::getgrgid_r(gid.0, grp, cbuf, cap, res)
})
}
}

/// Get a group by name.
Expand All @@ -3846,9 +3872,13 @@ impl Group {
Ok(c_str) => c_str,
Err(_nul_error) => return Ok(None),
};
Group::from_anything(|grp, cbuf, cap, res| unsafe {
libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res)
})
// SAFETY: `getgrnam_r` will write to `res` if it initializes the value
// at `grp`.
unsafe {
Group::from_anything(|grp, cbuf, cap, res| {
libc::getgrnam_r(name.as_ptr(), grp, cbuf, cap, res)
})
}
}
}
}
Expand Down