Skip to content

Commit

Permalink
Further changes recommended by @asomers
Browse files Browse the repository at this point in the history
  • Loading branch information
SolraBizna committed May 7, 2020
1 parent 6e00c73 commit d559aed
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,10 @@ pub fn renameat<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(old_dirfd: Option<Ra
Errno::result(res).map(drop)
}

fn wrap_readlink_result(mut v: Vec<u8>, res: ssize_t) -> Result<OsString> {
match Errno::result(res) {
Err(err) => Err(err),
Ok(len) => {
unsafe { v.set_len(len as usize) }
v.shrink_to_fit();
Ok(OsString::from_vec(v.to_vec()))
}
}
fn wrap_readlink_result(mut v: Vec<u8>, len: ssize_t) -> Result<OsString> {
unsafe { v.set_len(len as usize) }
v.shrink_to_fit();
Ok(OsString::from_vec(v.to_vec()))
}

fn readlink_maybe_at<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P,
Expand Down Expand Up @@ -220,8 +215,7 @@ fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P)
}
// Uh oh, the result is too long...
// Let's try to ask lstat how many bytes to allocate.
let reported_size = super::sys::stat::lstat(path)
.and_then(|x| Ok(x.st_size)).unwrap_or(0);
let reported_size = super::sys::stat::lstat(path).map_or(0, |x| x.st_size);
let mut try_size = if reported_size > 0 {
// Note: even if `lstat`'s apparently valid answer turns out to be
// wrong, we will still read the full symlink no matter what.
Expand All @@ -241,14 +235,11 @@ fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P)
}
else {
// Ugh! Still not big enough!
let next_size = try_size << 1;
if next_size < try_size {
match try_size.checked_shl(1) {
Some(next_size) => try_size = next_size,
// It's absurd that this would happen, but handle it sanely
// anyway.
break Err(super::Error::Sys(Errno::ENAMETOOLONG))
}
else {
try_size = next_size;
None => break Err(super::Error::Sys(Errno::ENAMETOOLONG))
}
}
}
Expand Down

0 comments on commit d559aed

Please sign in to comment.