Skip to content

Retry lookups with larger buffer sizes on ERANGE #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 48 additions & 12 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,23 @@ unsafe fn members(groups: *mut *mut c_char) -> Vec<OsString> {
/// ```
pub fn get_user_by_uid(uid: uid_t) -> Option<User> {
let mut passwd = unsafe { mem::zeroed::<c_passwd>() };
let mut buf = vec![0; 2048]; // TODO: Retry with larger buffer sizes
let mut buf = vec![0; 2048];
let mut result = ptr::null_mut::<c_passwd>();

#[cfg(feature = "logging")]
debug!("Running getpwuid_r for user #{}", uid);

unsafe {
libc::getpwuid_r(uid, &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result);
loop {
let r = unsafe {
libc::getpwuid_r(uid, &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result)
};

if r != libc::ERANGE {
break;
}

let newsize = buf.len().checked_mul(2)?;
buf.resize(newsize, 0);
}

if result.is_null() {
Expand Down Expand Up @@ -375,14 +384,23 @@ pub fn get_user_by_name<S: AsRef<OsStr> + ?Sized>(username: &S) -> Option<User>
};

let mut passwd = unsafe { mem::zeroed::<c_passwd>() };
let mut buf = vec![0; 2048]; // TODO: Retry with larger buffer sizes
let mut buf = vec![0; 2048];
let mut result = ptr::null_mut::<c_passwd>();

#[cfg(feature = "logging")]
debug!("Running getpwnam_r for user {:?}", username.as_ref());

unsafe {
libc::getpwnam_r(username.as_ptr(), &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result);
loop {
let r = unsafe {
libc::getpwnam_r(username.as_ptr(), &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result)
};

if r != libc::ERANGE {
break;
}

let newsize = buf.len().checked_mul(2)?;
buf.resize(newsize, 0);
}

if result.is_null() {
Expand Down Expand Up @@ -419,14 +437,23 @@ pub fn get_user_by_name<S: AsRef<OsStr> + ?Sized>(username: &S) -> Option<User>
/// ```
pub fn get_group_by_gid(gid: gid_t) -> Option<Group> {
let mut passwd = unsafe { mem::zeroed::<c_group>() };
let mut buf = vec![0; 2048]; // TODO: Retry with larger buffer sizes
let mut buf = vec![0; 2048];
let mut result = ptr::null_mut::<c_group>();

#[cfg(feature = "logging")]
debug!("Running getgruid_r for group #{}", gid);

unsafe {
libc::getgrgid_r(gid, &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result);
loop {
let r = unsafe {
libc::getgrgid_r(gid, &mut passwd, buf.as_mut_ptr(), buf.len(), &mut result)
};

if r != libc::ERANGE {
break;
}

let newsize = buf.len().checked_mul(2)?;
buf.resize(newsize, 0);
}

if result.is_null() {
Expand Down Expand Up @@ -472,14 +499,23 @@ pub fn get_group_by_name<S: AsRef<OsStr> + ?Sized>(groupname: &S) -> Option<Grou
};

let mut group = unsafe { mem::zeroed::<c_group>() };
let mut buf = vec![0; 2048]; // TODO: Retry with larger buffer sizes
let mut buf = vec![0; 2048];
let mut result = ptr::null_mut::<c_group>();

#[cfg(feature = "logging")]
debug!("Running getgrnam_r for group {:?}", groupname.as_ref());

unsafe {
libc::getgrnam_r(groupname.as_ptr(), &mut group, buf.as_mut_ptr(), buf.len(), &mut result);
loop {
let r = unsafe {
libc::getgrnam_r(groupname.as_ptr(), &mut group, buf.as_mut_ptr(), buf.len(), &mut result)
};

if r != libc::ERANGE {
break;
}

let newsize = buf.len().checked_mul(2)?;
buf.resize(newsize, 0);
}

if result.is_null() {
Expand Down