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

Use Wrapping for intended underflow of unsigned integer value. #369

Merged
merged 1 commit into from
May 6, 2016
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
8 changes: 6 additions & 2 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
#[inline]
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
let res = try!(path.with_nix_path(|cstr| {
// We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
unsafe { libc::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) }
// According to the POSIX specification, -1 is used to indicate that
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment is still relevant. What about you @fiveop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it; but then, we are not documenting all the other functions properly. So, for consistency I removed it. :)

If you do mind, I'll put it in again.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is kind of a weird value for a no-op and that line makes it a little clearer what we're trying to do with (Wrapping(0 as uid_t) - Wrapping(1 as uid_t)).0. Any opinions @kamalmarhubi?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add a comment.

Copy link
Member

@kamalmarhubi kamalmarhubi May 6, 2016

Choose a reason for hiding this comment

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

Any opinions @kamalmarhubi?

I remember asking for the comment back in #274 (comment) so... :-)

(I think it warrants the comment because it's a weird thing to do.)

// owner and group, respectively, are not to be changed. Since uid_t and
// gid_t are unsigned types, we use wrapping_sub to get '-1'.
unsafe { libc::chown(cstr.as_ptr(),
owner.unwrap_or((0 as uid_t).wrapping_sub(1)),
group.unwrap_or((0 as gid_t).wrapping_sub(1))) }
}));

Errno::result(res).map(drop)
Expand Down