Skip to content

Commit f47800c

Browse files
committed
Shrink some unsafe blocks; -> From traits use refs
1 parent 5c5929b commit f47800c

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

src/unistd.rs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2182,24 +2182,24 @@ pub struct User {
21822182
pub expire: libc::time_t
21832183
}
21842184

2185-
impl From<libc::passwd> for User {
2186-
fn from(pw: libc::passwd) -> User {
2185+
impl From<*mut libc::passwd> for User {
2186+
fn from(pw: *mut libc::passwd) -> User {
21872187
unsafe {
21882188
User {
2189-
name: CStr::from_ptr(pw.pw_name).to_string_lossy().into_owned(),
2190-
passwd: CString::new(CStr::from_ptr(pw.pw_passwd).to_bytes()).unwrap(),
2189+
name: CStr::from_ptr((*pw).pw_name).to_string_lossy().into_owned(),
2190+
passwd: CString::new(CStr::from_ptr((*pw).pw_passwd).to_bytes()).unwrap(),
21912191
#[cfg(not(target_os = "android"))]
2192-
gecos: CString::new(CStr::from_ptr(pw.pw_gecos).to_bytes()).unwrap(),
2193-
dir: PathBuf::from(OsStr::from_bytes(CStr::from_ptr(pw.pw_dir).to_bytes())),
2194-
shell: PathBuf::from(OsStr::from_bytes(CStr::from_ptr(pw.pw_shell).to_bytes())),
2195-
uid: Uid::from_raw(pw.pw_uid),
2196-
gid: Gid::from_raw(pw.pw_gid),
2192+
gecos: CString::new(CStr::from_ptr((*pw).pw_gecos).to_bytes()).unwrap(),
2193+
dir: PathBuf::from(OsStr::from_bytes(CStr::from_ptr((*pw).pw_dir).to_bytes())),
2194+
shell: PathBuf::from(OsStr::from_bytes(CStr::from_ptr((*pw).pw_shell).to_bytes())),
2195+
uid: Uid::from_raw((*pw).pw_uid),
2196+
gid: Gid::from_raw((*pw).pw_gid),
21972197
#[cfg(not(any(target_os = "linux", target_os = "android")))]
2198-
class: CString::new(CStr::from_ptr(pw.pw_class).to_bytes()).unwrap(),
2198+
class: CString::new(CStr::from_ptr((*pw).pw_class).to_bytes()).unwrap(),
21992199
#[cfg(not(any(target_os = "linux", target_os = "android")))]
2200-
change: pw.pw_change,
2200+
change: (*pw).pw_change,
22012201
#[cfg(not(any(target_os = "linux", target_os = "android")))]
2202-
expire: pw.pw_expire
2202+
expire: (*pw).pw_expire
22032203
}
22042204
}
22052205
}
@@ -2216,13 +2216,13 @@ pub struct Group {
22162216
pub mem: Vec<String>
22172217
}
22182218

2219-
impl From<libc::group> for Group {
2220-
fn from(gr: libc::group) -> Group {
2219+
impl From<*mut libc::group> for Group {
2220+
fn from(gr: *mut libc::group) -> Group {
22212221
unsafe {
22222222
Group {
2223-
name: CStr::from_ptr(gr.gr_name).to_string_lossy().into_owned(),
2224-
gid: Gid::from_raw(gr.gr_gid),
2225-
mem: Group::members(gr.gr_mem)
2223+
name: CStr::from_ptr((*gr).gr_name).to_string_lossy().into_owned(),
2224+
gid: Gid::from_raw((*gr).gr_gid),
2225+
mem: Group::members((*gr).gr_mem)
22262226
}
22272227
}
22282228
}
@@ -2332,20 +2332,24 @@ impl Queryable<UserQuery> for User {
23322332
let mut pwd: libc::passwd = unsafe { mem::zeroed() };
23332333
let mut res = ptr::null_mut();
23342334

2335-
let i = unsafe {
2336-
Errno::clear();
2335+
let i = {
2336+
unsafe { Errno::clear(); }
23372337

23382338
match q {
23392339
UserQuery::UidWithBufsize(Uid(uid), _) |
23402340
UserQuery::Uid(Uid(uid)) => {
2341-
libc::getpwuid_r(uid, &mut pwd,
2342-
cbuf.as_mut_ptr(),
2343-
cbuf.len(), &mut res)
2341+
unsafe {
2342+
libc::getpwuid_r(uid, &mut pwd,
2343+
cbuf.as_mut_ptr(),
2344+
cbuf.len(), &mut res)
2345+
}
23442346
},
23452347
UserQuery::NameWithBufsize(name, _) |
23462348
UserQuery::Name(name) => {
2347-
libc::getpwnam_r(CString::new(name).unwrap().as_ptr(),
2348-
&mut pwd, cbuf.as_mut_ptr(), cbuf.len(), &mut res)
2349+
unsafe {
2350+
libc::getpwnam_r(CString::new(name).unwrap().as_ptr(),
2351+
&mut pwd, cbuf.as_mut_ptr(), cbuf.len(), &mut res)
2352+
}
23492353
},
23502354
}
23512355

@@ -2354,7 +2358,7 @@ impl Queryable<UserQuery> for User {
23542358
match i {
23552359
0 => {
23562360
if !res.is_null() {
2357-
unsafe { Some(Ok(User::from(*res))) }
2361+
Some(Ok(User::from(res)))
23582362
} else {
23592363
None
23602364
}
@@ -2379,20 +2383,24 @@ impl Queryable<GroupQuery> for Group {
23792383
let mut grp: libc::group = unsafe { mem::zeroed() };
23802384
let mut res = ptr::null_mut();
23812385

2382-
let i = unsafe {
2383-
Errno::clear();
2386+
let i = {
2387+
unsafe { Errno::clear(); }
23842388

23852389
match q {
23862390
GroupQuery::GidWithBufsize(Gid(gid), _) |
2387-
GroupQuery::Gid(Gid(gid)) => {
2388-
libc::getgrgid_r(gid, &mut grp,
2389-
cbuf.as_mut_ptr(),
2390-
cbuf.len(), &mut res)
2391+
GroupQuery::Gid(Gid(gid)) => {
2392+
unsafe {
2393+
libc::getgrgid_r(gid, &mut grp,
2394+
cbuf.as_mut_ptr(),
2395+
cbuf.len(), &mut res)
2396+
}
23912397
},
23922398
GroupQuery::NameWithBufsize(name, _) |
23932399
GroupQuery::Name(name) => {
2394-
libc::getgrnam_r(CString::new(name).unwrap().as_ptr(),
2395-
&mut grp, cbuf.as_mut_ptr(), cbuf.len(), &mut res)
2400+
unsafe {
2401+
libc::getgrnam_r(CString::new(name).unwrap().as_ptr(),
2402+
&mut grp, cbuf.as_mut_ptr(), cbuf.len(), &mut res)
2403+
}
23962404
},
23972405
}
23982406

@@ -2401,7 +2409,7 @@ impl Queryable<GroupQuery> for Group {
24012409
match i {
24022410
0 => {
24032411
if !res.is_null() {
2404-
unsafe { Some(Ok(Group::from(*res))) }
2412+
Some(Ok(Group::from(res)))
24052413
} else {
24062414
None
24072415
}
@@ -2480,7 +2488,7 @@ mod usergroupiter {
24802488

24812489
match i {
24822490
0 if !res.is_null() => {
2483-
unsafe { Some(Ok(User::from(*res))) }
2491+
Some(Ok(User::from(res)))
24842492
},
24852493
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
24862494
_ => None
@@ -2545,7 +2553,7 @@ mod usergroupiter {
25452553

25462554
match i {
25472555
0 if !res.is_null() => {
2548-
unsafe { Some(Ok(Group::from(*res))) }
2556+
Some(Ok(Group::from(res)))
25492557
},
25502558
libc::ERANGE => { Some(Err(Error::Sys(Errno::last()))) },
25512559
_ => None

0 commit comments

Comments
 (0)