Skip to content

Commit 150579c

Browse files
bors[bot]ocadarumavitalydasomers
authored
1538: posix_fadvise doesn't return -1 as sentinel value r=asomers a=ocadaruma ## Summary - `posix_fadvise(2)` does return error number directly (i.e. not through `errno`) * refs: https://man7.org/linux/man-pages/man2/posix_fadvise.2.html , https://man7.org/linux/man-pages/man2/posix_fadvise.2.html - However `posix_fadvise`-binding uses `Errno::result` to translate the error now, which is mis-use. 1545: Fix memory unsafety in unistd::getgrouplist r=asomers a=asomers Fixes #1541 1546: Revert "Expose SockAddr::from_raw_sockaddr" r=asomers a=asomers This reverts commit ed43d2c. As discussed in #1544 the API of this function needs to change. For now, revert the PR that made it public, because it has not yet been included in any release. Co-authored-by: Haruki Okada <ocadaruma@gmail.com> Co-authored-by: vitalyd <vitalyd@gmail.com> Co-authored-by: Alan Somers <asomers@gmail.com>
4 parents 759b34a + a607034 + 1671edc + 8d6c2b3 commit 150579c

File tree

6 files changed

+27
-17
lines changed

6 files changed

+27
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,17 @@ This project adheres to [Semantic Versioning](https://semver.org/).
8383

8484
### Fixed
8585

86+
- `posix_fadvise` now returns errors in the conventional way, rather than as a
87+
non-zero value in `Ok()`.
88+
(#[1538](https://github.com/nix-rust/nix/pull/1538))
8689
- Added more errno definitions for better backwards compatibility with
8790
Nix 0.21.0.
8891
(#[1467](https://github.com/nix-rust/nix/pull/1467))
8992
- Fixed potential undefined behavior in `Signal::try_from` on some platforms.
9093
(#[1484](https://github.com/nix-rust/nix/pull/1484))
94+
- Fixed buffer overflow in `unistd::getgrouplist`.
95+
(#[1545](https://github.com/nix-rust/nix/pull/1545))
96+
9197

9298
### Removed
9399

src/fcntl.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,9 +667,14 @@ mod posix_fadvise {
667667
offset: libc::off_t,
668668
len: libc::off_t,
669669
advice: PosixFadviseAdvice,
670-
) -> Result<libc::c_int> {
670+
) -> Result<()> {
671671
let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) };
672-
Errno::result(res)
672+
673+
if res == 0 {
674+
Ok(())
675+
} else {
676+
Err(Errno::from_i32(res))
677+
}
673678
}
674679
}
675680

src/ifaddrs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl InterfaceAddress {
4646
/// Create an `InterfaceAddress` from the libc struct.
4747
fn from_libc_ifaddrs(info: &libc::ifaddrs) -> InterfaceAddress {
4848
let ifname = unsafe { ffi::CStr::from_ptr(info.ifa_name) };
49-
let address = unsafe { SockAddr::from_raw_sockaddr(info.ifa_addr) };
50-
let netmask = unsafe { SockAddr::from_raw_sockaddr(info.ifa_netmask) };
49+
let address = unsafe { SockAddr::from_libc_sockaddr(info.ifa_addr) };
50+
let netmask = unsafe { SockAddr::from_libc_sockaddr(info.ifa_netmask) };
5151
let mut addr = InterfaceAddress {
5252
interface_name: ifname.to_string_lossy().to_string(),
5353
flags: InterfaceFlags::from_bits_truncate(info.ifa_flags as i32),
@@ -59,9 +59,9 @@ impl InterfaceAddress {
5959

6060
let ifu = get_ifu_from_sockaddr(info);
6161
if addr.flags.contains(InterfaceFlags::IFF_POINTOPOINT) {
62-
addr.destination = unsafe { SockAddr::from_raw_sockaddr(ifu) };
62+
addr.destination = unsafe { SockAddr::from_libc_sockaddr(ifu) };
6363
} else if addr.flags.contains(InterfaceFlags::IFF_BROADCAST) {
64-
addr.broadcast = unsafe { SockAddr::from_raw_sockaddr(ifu) };
64+
addr.broadcast = unsafe { SockAddr::from_libc_sockaddr(ifu) };
6565
}
6666

6767
addr

src/sys/socket/addr.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ impl SockAddr {
802802
/// unsafe because it takes a raw pointer as argument. The caller must
803803
/// ensure that the pointer is valid.
804804
#[cfg(not(target_os = "fuchsia"))]
805-
pub unsafe fn from_raw_sockaddr(addr: *const libc::sockaddr) -> Option<SockAddr> {
805+
pub(crate) unsafe fn from_libc_sockaddr(addr: *const libc::sockaddr) -> Option<SockAddr> {
806806
if addr.is_null() {
807807
None
808808
} else {
@@ -1381,7 +1381,7 @@ mod tests {
13811381
fn test_macos_loopback_datalink_addr() {
13821382
let bytes = [20i8, 18, 1, 0, 24, 3, 0, 0, 108, 111, 48, 0, 0, 0, 0, 0];
13831383
let sa = bytes.as_ptr() as *const libc::sockaddr;
1384-
let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) };
1384+
let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) };
13851385
assert!(_sock_addr.is_none());
13861386
}
13871387

@@ -1396,7 +1396,7 @@ mod tests {
13961396
let bytes = [20i8, 18, 7, 0, 6, 3, 6, 0, 101, 110, 48, 24, 101, -112, -35, 76, -80];
13971397
let ptr = bytes.as_ptr();
13981398
let sa = ptr as *const libc::sockaddr;
1399-
let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) };
1399+
let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) };
14001400

14011401
assert!(_sock_addr.is_some());
14021402

@@ -1418,7 +1418,7 @@ mod tests {
14181418
let bytes = [25u8, 0, 0, 0, 6, 0, 6, 0, 24, 101, 144, 221, 76, 176];
14191419
let ptr = bytes.as_ptr();
14201420
let sa = ptr as *const libc::sockaddr;
1421-
let _sock_addr = unsafe { SockAddr::from_raw_sockaddr(sa) };
1421+
let _sock_addr = unsafe { SockAddr::from_libc_sockaddr(sa) };
14221422

14231423
assert!(_sock_addr.is_some());
14241424

src/unistd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,8 +1540,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
15401540
Ok(None) | Err(_) => <c_int>::max_value(),
15411541
};
15421542
use std::cmp::min;
1543-
let mut ngroups = min(ngroups_max, 8);
1544-
let mut groups = Vec::<Gid>::with_capacity(ngroups as usize);
1543+
let mut groups = Vec::<Gid>::with_capacity(min(ngroups_max, 8) as usize);
15451544
cfg_if! {
15461545
if #[cfg(any(target_os = "ios", target_os = "macos"))] {
15471546
type getgrouplist_group_t = c_int;
@@ -1551,6 +1550,7 @@ pub fn getgrouplist(user: &CStr, group: Gid) -> Result<Vec<Gid>> {
15511550
}
15521551
let gid: gid_t = group.into();
15531552
loop {
1553+
let mut ngroups = groups.capacity() as i32;
15541554
let ret = unsafe {
15551555
libc::getgrouplist(user.as_ptr(),
15561556
gid as getgrouplist_group_t,

test/test_fcntl.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,17 +473,16 @@ mod test_posix_fadvise {
473473
fn test_success() {
474474
let tmp = NamedTempFile::new().unwrap();
475475
let fd = tmp.as_raw_fd();
476-
let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED).unwrap();
476+
let res = posix_fadvise(fd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED);
477477

478-
assert_eq!(res, 0);
478+
assert!(res.is_ok());
479479
}
480480

481481
#[test]
482482
fn test_errno() {
483483
let (rd, _wr) = pipe().unwrap();
484-
let errno = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED)
485-
.unwrap();
486-
assert_eq!(errno, Errno::ESPIPE as i32);
484+
let res = posix_fadvise(rd as RawFd, 0, 100, PosixFadviseAdvice::POSIX_FADV_WILLNEED);
485+
assert_eq!(res, Err(Errno::ESPIPE));
487486
}
488487
}
489488

0 commit comments

Comments
 (0)