Skip to content

Commit 07a296a

Browse files
committed
refactor(sort): replace unsafe libc::fcntl with rustix::io::fcntl_getfd
1 parent ab65949 commit 07a296a

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

src/uu/sort/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ ctrlc = { workspace = true }
5151

5252
[target.'cfg(unix)'.dependencies]
5353
libc = { workspace = true }
54-
rustix = { workspace = true, features = ["process", "param"] }
54+
rustix = { workspace = true, features = ["process", "param", "fs"] }
5555

5656
[dev-dependencies]
5757
divan = { workspace = true }

src/uu/sort/src/sort.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,10 @@ pub(crate) fn current_open_fd_count() -> Option<usize> {
14371437

14381438
let mut count = 0usize;
14391439
for fd in 0..limit {
1440-
let fd = fd as libc::c_int;
1441-
// Probe with libc::fcntl because the fd may be invalid.
1442-
if unsafe { libc::fcntl(fd, libc::F_GETFD) } != -1 {
1440+
let fd = fd as std::os::fd::RawFd;
1441+
// SAFETY: We are only probing whether the fd is valid via fcntl_getfd;
1442+
// the borrowed fd is not used beyond this call.
1443+
if rustix::io::fcntl_getfd(unsafe { std::os::fd::BorrowedFd::borrow_raw(fd) }).is_ok() {
14431444
count = count.saturating_add(1);
14441445
}
14451446
}

0 commit comments

Comments
 (0)