Skip to content

Commit b6820d1

Browse files
committed
refactor: use libc::fcntl for probing invalid fds in sort utility
Update current_open_fd_count to use unsafe libc::fcntl instead of nix::fcntl, ensuring -1 return for invalid file descriptors, which is more reliable for counting open fds and avoids potential errors. Added GETFD to spell-checker ignore list.
1 parent 39f5e72 commit b6820d1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/uu/sort/src/sort.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html
88
// https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
99

10-
// spell-checker:ignore (misc) HFKJFK Mbdfhn getrlimit RLIMIT_NOFILE rlim bigdecimal extendedbigdecimal hexdigit behaviour keydef
10+
// spell-checker:ignore (misc) HFKJFK Mbdfhn getrlimit RLIMIT_NOFILE rlim bigdecimal extendedbigdecimal hexdigit behaviour keydef GETFD
1111

1212
mod buffer_hint;
1313
mod check;
@@ -1109,7 +1109,7 @@ pub(crate) fn fd_soft_limit() -> Option<usize> {
11091109

11101110
#[cfg(unix)]
11111111
pub(crate) fn current_open_fd_count() -> Option<usize> {
1112-
use nix::fcntl::{FcntlArg, fcntl};
1112+
use nix::libc;
11131113

11141114
fn count_dir(path: &str) -> Option<usize> {
11151115
let entries = std::fs::read_dir(path).ok()?;
@@ -1135,7 +1135,9 @@ pub(crate) fn current_open_fd_count() -> Option<usize> {
11351135

11361136
let mut count = 0usize;
11371137
for fd in 0..limit {
1138-
if fcntl(fd as i32, FcntlArg::F_GETFD).is_ok() {
1138+
let fd = fd as libc::c_int;
1139+
// Probe with libc::fcntl because the fd may be invalid.
1140+
if unsafe { libc::fcntl(fd, libc::F_GETFD) } != -1 {
11391141
count = count.saturating_add(1);
11401142
}
11411143
}

0 commit comments

Comments
 (0)