Fix bug that panics when getting group info

I'm not sure if this a Rust change or something I've introduced...
This commit is contained in:
Ben S 2014-11-24 01:13:58 +00:00
parent 3066f2f840
commit b7e7bc7057

View File

@ -1,5 +1,6 @@
use std::string::raw::from_buf;
use std::ptr::read;
use std::ptr;
use std::collections::HashMap;
mod c {
@ -24,10 +25,11 @@ mod c {
pub pw_gecos: *const c_char, // full name
pub pw_dir: *const c_char, // login dir
pub pw_shell: *const c_char, // login shell
pub pw_expire: time_t // password expiry time
pub pw_expire: time_t, // password expiry time
}
#[repr(C)]
#[deriving(Show)]
pub struct c_group {
pub gr_name: *const c_char, // group name
pub gr_passwd: *const c_char, // password
@ -100,7 +102,7 @@ impl Unix {
}
}
fn group_membership(group: *const *const i8, uname: &String) -> bool {
fn group_membership(group: *const *const c::c_char, uname: &String) -> bool {
let mut i = 0;
// The list of members is a pointer to a pointer of
@ -110,18 +112,21 @@ impl Unix {
// The second call will return None if it's a null pointer.
loop {
match unsafe { group.offset(i).as_ref().unwrap().as_ref() } {
Some(username) => {
if unsafe { from_buf(*username as *const u8) } == *uname {
return true;
}
}
None => {
match unsafe { group.offset(i).as_ref() } {
Some(&username) => {
if username == ptr::null() {
return false;
}
if unsafe { from_buf(username as *const u8) } == *uname {
return true;
}
else {
i += 1;
}
},
None => return false,
}
}
}
pub fn load_group(&mut self, gid: u32) {
@ -132,11 +137,12 @@ impl Unix {
},
Some(r) => {
let group_name = unsafe { Some(from_buf(r.gr_name as *const u8)) };
if !self.groups.contains_key(&gid) {
self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username));
}
self.group_names.insert(gid, group_name);
}
}
}
}