std::unix::os::home_dir: fallback's optimisation.
we're using a guaranteed initialised field on success.
This commit is contained in:
parent
d4cc01c2f2
commit
bd9ce3e074
@ -738,16 +738,17 @@ unsafe fn fallback() -> Option<OsString> {
|
||||
n => n as usize,
|
||||
};
|
||||
let mut buf = Vec::with_capacity(amt);
|
||||
let mut passwd: libc::passwd = mem::zeroed();
|
||||
let mut p = mem::MaybeUninit::<libc::passwd>::uninit();
|
||||
let mut result = ptr::null_mut();
|
||||
match libc::getpwuid_r(
|
||||
libc::getuid(),
|
||||
&mut passwd,
|
||||
p.as_mut_ptr(),
|
||||
buf.as_mut_ptr(),
|
||||
buf.capacity(),
|
||||
&mut result,
|
||||
) {
|
||||
0 if !result.is_null() => {
|
||||
let passwd = p.assume_init();
|
||||
let ptr = passwd.pw_dir as *const _;
|
||||
let bytes = CStr::from_ptr(ptr).to_bytes().to_vec();
|
||||
Some(OsStringExt::from_vec(bytes))
|
||||
|
Loading…
Reference in New Issue
Block a user