summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManos Pitsidianakis <el13635@mail.ntua.gr>2019-10-25 08:47:15 +0300
committerManos Pitsidianakis <el13635@mail.ntua.gr>2019-10-25 08:47:15 +0300
commit59190550b93840d8d19d96b86dc0a2749ee617c7 (patch)
tree82852e23449f2d3a29855051f4d571fe70638590
parent51f37839aa8b7b8a3888377ecd8e50791b2bd590 (diff)
Prevent data loss when reading username
buffer was local to getpwuid, and C string data was written to the buffer. When username() got the return value the original buffer was lost so pass it by reference.
-rw-r--r--src/ui.rs27
1 files changed, 11 insertions, 16 deletions
diff --git a/src/ui.rs b/src/ui.rs
index 5c10408..3aff635 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -47,9 +47,8 @@ pub mod username {
use libc;
use std::ptr::null_mut;
/* taken from whoami-0.1.1 */
- fn getpwuid(pw_uid: u32) -> libc::passwd {
+ fn getpwuid(pw_uid: u32, buffer: &mut [i8; 16384]) -> libc::passwd {
let mut pwentp = null_mut();
- let mut buffer = [0i8; 16384]; // from the man page
#[cfg(any(
target_os = "macos",
target_os = "ios",
@@ -72,7 +71,7 @@ pub mod username {
pw_expire: 0,
};
unsafe {
- libc::getpwuid_r(pw_uid, &mut pwent, &mut buffer[0], 16384, &mut pwentp);
+ libc::getpwuid_r(pw_uid, &mut pwent, buffer, 16384, &mut pwentp);
}
pwent
@@ -90,29 +89,25 @@ pub mod username {
};
unsafe {
- libc::getpwuid_r(pw_uid, &mut pwent, &mut buffer[0], 16384, &mut pwentp);
+ libc::getpwuid_r(pw_uid, &mut pwent, buffer.as_mut_ptr(), 16384, &mut pwentp);
}
pwent
}
}
- fn ptr_to_string(name: *mut i8) -> String {
- let uname = name as *mut _ as *mut u8;
- let s;
- let string;
+ pub fn username(uid: u32) -> String {
+ let mut buffer = [0i8; 16384]; // from the man page
+ let pwent = getpwuid(uid, &mut buffer);
+ let string;
unsafe {
- s = ::std::slice::from_raw_parts(uname, libc::strlen(name));
- string = String::from_utf8_lossy(s).to_string();
+ string = ::std::ffi::CStr::from_ptr(pwent.pw_name)
+ .to_str()
+ .unwrap_or_else(|_| "")
+ .to_string();
}
string
}
-
- pub fn username(uid: u32) -> String {
- let pwent = getpwuid(uid);
-
- ptr_to_string(pwent.pw_name)
- }
}