diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2023-03-08 00:00:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 00:00:50 -0500 |
commit | 10d7226b19345d6808d03ac34be3f82a85229a4d (patch) | |
tree | 229c5b4dbc124b6c0dc261729fc15150597d2be4 /src/app/data_harvester | |
parent | 9c197d0cf68a7dbd30ac9f35a9563ef118c3a596 (diff) |
other: deny missing safety docs and add them (#1053)
Diffstat (limited to 'src/app/data_harvester')
-rw-r--r-- | src/app/data_harvester/processes/macos/sysctl_bindings.rs | 7 | ||||
-rw-r--r-- | src/app/data_harvester/processes/unix.rs | 17 |
2 files changed, 15 insertions, 9 deletions
diff --git a/src/app/data_harvester/processes/macos/sysctl_bindings.rs b/src/app/data_harvester/processes/macos/sysctl_bindings.rs index e56d79b8..420c6a2e 100644 --- a/src/app/data_harvester/processes/macos/sysctl_bindings.rs +++ b/src/app/data_harvester/processes/macos/sysctl_bindings.rs @@ -264,12 +264,13 @@ pub(crate) struct eproc { /// Obtains the [`kinfo_proc`] given a process PID. /// -/// From [heim](https://github.com/heim-rs/heim/blob/master/heim-process/src/sys/macos/bindings/process.rs#L235). +/// Based on the implementation from [heim](https://github.com/heim-rs/heim/blob/master/heim-process/src/sys/macos/bindings/process.rs#L235). pub(crate) fn kinfo_process(pid: Pid) -> Result<kinfo_proc> { let mut name: [i32; 4] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid]; let mut size = mem::size_of::<kinfo_proc>(); let mut info = mem::MaybeUninit::<kinfo_proc>::uninit(); + // SAFETY: libc binding, we assume all arguments are valid. let result = unsafe { libc::sysctl( name.as_mut_ptr(), @@ -290,6 +291,10 @@ pub(crate) fn kinfo_process(pid: Pid) -> Result<kinfo_proc> { bail!("failed to get process for pid {pid}"); } + // SAFETY: info is initialized if result succeeded and returned a non-negative result. If sysctl failed, it returns + // -1 with errno set. + // + // Source: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/sysctl.3.html unsafe { Ok(info.assume_init()) } } diff --git a/src/app/data_harvester/processes/unix.rs b/src/app/data_harvester/processes/unix.rs index 75f70bec..3e459e06 100644 --- a/src/app/data_harvester/processes/unix.rs +++ b/src/app/data_harvester/processes/unix.rs @@ -18,15 +18,16 @@ impl UserTable { 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(); - self.uid_user_mapping.insert(uid, username.clone()); + Err(error::BottomError::QueryError("Missing passwd".into())) + } else { + // SAFETY: We return early if passwd is null. + let username = unsafe { std::ffi::CStr::from_ptr((*passwd).pw_name) } + .to_str()? + .to_string(); + self.uid_user_mapping.insert(uid, username.clone()); - Ok(username) + Ok(username) + } } } } |