bug: Fix getpwuid segfault (#440)

Fixes a rare segfault if a uid does not have a passwd entry. The unsafe block at https://github.com/ClementTsang/bottom/blob/master/src/app/data_harvester/processes.rs#L137 can return a null pointer as specified at https://www.gnu.org/software/libc/manual/html_node/Lookup-User.html.
This commit is contained in:
Zeb Piasecki 2021-03-23 19:37:28 -04:00 committed by GitHub
parent b8fb78a769
commit c79956843e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 0 deletions

View File

@ -134,7 +134,13 @@ impl UserTable {
if let Some(user) = self.uid_user_mapping.get(&uid) {
Ok(user.clone())
} else {
// SAFETY: getpwuid returns a null pointer if no passwd entry is found for the uid
let passwd = unsafe { libc::getpwuid(uid) };
if passwd.is_null() {
return Err(error::BottomError::QueryError("Missing passwd".into()));
}
let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) }
.to_str()?
.to_string();