Skip to content
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

fix readlink/readlinkat to return too long only when it is long #1109

Merged
merged 8 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ This project adheres to [Semantic Versioning](http://semver.org/).
```rust
use nix::fcntl::{readlink, readlinkat};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't important to readers. You can suppress it with a leading #, and delete the following blank line.


readlink(&path);
readlinkat(dirfd, &path);
// the buffer argument of `readlink` and `readlinkat` has been removed,
// and the return value is now an owned type (`OsString`).
// Existing code can be updated by removing the buffer argument
// and removing any clone or similar operation on the output

// old code `readlink(&path, &mut buf)` can be replaced with the following
readlink(&path); // this returns OsString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code would be more self-documenting like this:

let _: OsString = readlink(&path);


// old code `readlinkat(dirfd, &path, &mut buf)` can be replaced with the following
readlinkat(dirfd, &path); // this returns OsString
```

### Fixed
Expand Down
10 changes: 4 additions & 6 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,19 @@ fn wrap_readlink_result(v: &mut Vec<u8>, res: ssize_t) -> Result<OsString> {
}

pub fn readlink<'a, P: ?Sized + NixPath>(path: &P) -> Result<OsString> {
let len = libc::PATH_MAX as usize;
let mut v = Vec::with_capacity(len);
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
let res = path.with_nix_path(|cstr| {
unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, len as size_t) }
unsafe { libc::readlink(cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }
})?;

wrap_readlink_result(&mut v, res)
}


pub fn readlinkat<'a, P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> {
let len = libc::PATH_MAX as usize;
let mut v = Vec::with_capacity(len);
let mut v = Vec::with_capacity(libc::PATH_MAX as usize);
let res = path.with_nix_path(|cstr| {
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, len as size_t) }
unsafe { libc::readlinkat(dirfd, cstr.as_ptr(), v.as_mut_ptr() as *mut c_char, v.capacity() as size_t) }
})?;

wrap_readlink_result(&mut v, res)
Expand Down