Skip to content

Commit 2139c90

Browse files
committed
Use Wrapping for intended underflow of unsigned integer value.
1 parent 273347d commit 2139c90

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/unistd.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ pub fn chdir<P: ?Sized + NixPath>(path: &P) -> Result<()> {
114114
#[inline]
115115
pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<uid_t>, group: Option<gid_t>) -> Result<()> {
116116
let res = try!(path.with_nix_path(|cstr| {
117-
// We use `0 - 1` to get `-1 : {u,g}id_t` which is specified as the no-op value for chown(3).
118-
unsafe { libc::chown(cstr.as_ptr(), owner.unwrap_or(0 - 1), group.unwrap_or(0 - 1)) }
117+
// According to the POSIX specification, -1 is used to indicate that
118+
// owner and group, respectively, are not to be changed. Since uid_t and
119+
// gid_t are unsigned types, we use wrapping_sub to get '-1'.
120+
unsafe { libc::chown(cstr.as_ptr(),
121+
owner.unwrap_or((0 as uid_t).wrapping_sub(1)),
122+
group.unwrap_or((0 as gid_t).wrapping_sub(1))) }
119123
}));
120124

121125
Errno::result(res).map(drop)

0 commit comments

Comments
 (0)