whoami: Restrict scope of unsafe

Co-authored-by: Jan Scheer <jhscheer@users.noreply.github.com>
This commit is contained in:
Jan Verbeek 2021-09-15 15:37:15 +02:00 committed by GitHub
parent 0a3785bf84
commit 5bb56ec528
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -20,10 +20,8 @@ pub fn get_username() -> io::Result<OsString> {
let mut buffer = [0_u16; BUF_LEN as usize];
let mut len = BUF_LEN;
// SAFETY: buffer.len() == len
unsafe {
if winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) == 0 {
return Err(io::Error::last_os_error());
}
if unsafe { winbase::GetUserNameW(buffer.as_mut_ptr(), &mut len) } == 0 {
return Err(io::Error::last_os_error());
}
Ok(OsString::from_wide(&buffer[..len as usize - 1]))
}