From 1c03d47dac04bc074a8de05b84ac9149aa3612a5 Mon Sep 17 00:00:00 2001 From: Albert S Date: Thu, 9 Jul 2020 00:11:50 +0200 Subject: [PATCH] Fixed getpwnam problems related to ptr lifecycle --- src/main.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3b84dcd..abf579d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -75,11 +75,13 @@ fn getpwnam(username: &str) -> std::io::Result { fn getstr(str: *mut libc::c_char) -> String { unsafe { CStr::from_ptr(str).to_string_lossy().into_owned() } } - unsafe { - let pwnamresult: *mut passwd = libc::getpwnam(CString::new(username).unwrap().as_ptr()); + let username_c = CString::new(username).unwrap(); + let username_ptr = username_c.as_ptr(); + let pwnamresult: *mut libc::passwd = unsafe { libc::getpwnam(username_ptr) }; if pwnamresult.is_null() { - return Err(Error::last_os_error()); + return Err(Error::new(Error::last_os_error().kind(),"Lookup of user failed: ".to_owned() + &Error::last_os_error().to_string())); } + unsafe { Ok(Passwd { pw_name: getstr((*pwnamresult).pw_name), pw_passwd: getstr((*pwnamresult).pw_passwd),