Skip to content

Commit

Permalink
Auto merge of rust-lang#3356 - magicant:fd_setsize_type, r=JohnTitor
Browse files Browse the repository at this point in the history
Change `FD_SETSIZE` to `c_int`

`FD_SETSIZE` defines an upper bound for file descriptors that can be added to `fd_set`. It is common to check if a file descriptor is less than `FD_SETSIZE` before adding it to an `fd_set`. See the example below:

```rust
fn main() {
    let path = std::ffi::CStr::from_bytes_with_nul(b"/dev/null\0").unwrap();
    let fd = unsafe { libc::open(path.as_ptr(), libc::O_RDONLY) };
    if fd < 0 {
        eprint!("open failed\n");
        return;
    }

    let mut set = std::mem::MaybeUninit::<libc::fd_set>::uninit();
    unsafe { libc::FD_ZERO(set.as_mut_ptr()) }
    if fd < libc::FD_SETSIZE { // <- type mismatch!
        unsafe { libc::FD_SET(fd, set.as_mut_ptr()) }
    } else {
        eprint!("too large fd\n");
    }
}
```

Unfortunately, this example does not compile because of a type mismatch. `fd` is `c_int` while `FD_SETSIZE` is `usize` (or `size_t`).
Since `FD_SETSIZE` represents the max file descriptor + 1, I think it should have the same type as file descriptors. This pull request modifies the type to `c_int` and adds casts where needed.
  • Loading branch information
bors committed Jan 8, 2024
2 parents a0d9be1 + 500365e commit 7412a8b
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ s! {
}

pub struct fd_set {
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
}

pub struct tm {
Expand Down Expand Up @@ -1827,7 +1827,7 @@ pub const SS_DISABLE: ::c_int = 2;

pub const PATH_MAX: ::c_int = 4096;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const EPOLLIN: ::c_int = 0x1;
pub const EPOLLPRI: ::c_int = 0x2;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/aix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2118,7 +2118,7 @@ pub const POWER_8: ::c_int = 0x10000;
pub const POWER_9: ::c_int = 0x20000;

// sys/time.h
pub const FD_SETSIZE: usize = 65534;
pub const FD_SETSIZE: ::c_int = 65534;
pub const TIMEOFDAY: ::c_int = 9;
pub const CLOCK_REALTIME: ::clockid_t = TIMEOFDAY as clockid_t;
pub const CLOCK_MONOTONIC: ::clockid_t = 10;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4384,7 +4384,7 @@ pub const OS_SIGNPOST_INTERVAL_END: ::os_signpost_type_t = 0x02;
pub const MINSIGSTKSZ: ::size_t = 32768;
pub const SIGSTKSZ: ::size_t = 131072;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const ST_NOSUID: ::c_ulong = 2;

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ pub const SCHED_FIFO: ::c_int = 1;
pub const SCHED_OTHER: ::c_int = 2;
pub const SCHED_RR: ::c_int = 3;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const ST_NOSUID: ::c_ulong = 2;

Expand Down
4 changes: 2 additions & 2 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ s! {
pub struct fd_set {
#[cfg(all(target_pointer_width = "64",
any(target_os = "freebsd", target_os = "dragonfly")))]
fds_bits: [i64; FD_SETSIZE / 64],
fds_bits: [i64; FD_SETSIZE as usize / 64],
#[cfg(not(all(target_pointer_width = "64",
any(target_os = "freebsd", target_os = "dragonfly"))))]
fds_bits: [i32; FD_SETSIZE / 32],
fds_bits: [i32; FD_SETSIZE as usize / 32],
}

pub struct tm {
Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/netbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ pub const _SC_SCHED_RT_TS: ::c_int = 2001;
pub const _SC_SCHED_PRI_MIN: ::c_int = 2002;
pub const _SC_SCHED_PRI_MAX: ::c_int = 2003;

pub const FD_SETSIZE: usize = 0x100;
pub const FD_SETSIZE: ::c_int = 0x100;

pub const ST_NOSUID: ::c_ulong = 8;

Expand Down
2 changes: 1 addition & 1 deletion src/unix/bsd/netbsdlike/openbsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,7 +1422,7 @@ pub const _SC_AVPHYS_PAGES: ::c_int = 501;
pub const _SC_NPROCESSORS_CONF: ::c_int = 502;
pub const _SC_NPROCESSORS_ONLN: ::c_int = 503;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const SCHED_FIFO: ::c_int = 1;
pub const SCHED_OTHER: ::c_int = 2;
Expand Down
2 changes: 1 addition & 1 deletion src/unix/haiku/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ pub const SA_ONESHOT: ::c_int = SA_RESETHAND;
pub const SS_ONSTACK: ::c_int = 0x1;
pub const SS_DISABLE: ::c_int = 0x2;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const RTLD_LOCAL: ::c_int = 0x0;
pub const RTLD_NOW: ::c_int = 0x1;
Expand Down
8 changes: 4 additions & 4 deletions src/unix/hurd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ pub const SHM_UNLOCK: ::c_int = 12;
pub const STDIN_FILENO: ::c_int = 0;
pub const STDOUT_FILENO: ::c_int = 1;
pub const STDERR_FILENO: ::c_int = 2;
pub const __FD_SETSIZE: usize = 256;
pub const __FD_SETSIZE: ::c_int = 256;
pub const R_OK: ::c_int = 4;
pub const W_OK: ::c_int = 2;
pub const X_OK: ::c_int = 1;
Expand Down Expand Up @@ -1256,7 +1256,7 @@ pub const PDP_ENDIAN: usize = 3412;
pub const BYTE_ORDER: usize = 1234;

// sys/select.h
pub const FD_SETSIZE: usize = 256;
pub const FD_SETSIZE: ::c_int = 256;
pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 32;
pub const __SIZEOF_PTHREAD_ATTR_T: usize = 32;
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 28;
Expand Down Expand Up @@ -1588,8 +1588,8 @@ pub const _POSIX_MQ_OPEN_MAX: usize = 8;
pub const _POSIX_MQ_PRIO_MAX: usize = 32;
pub const _POSIX_NAME_MAX: usize = 14;
pub const _POSIX_NGROUPS_MAX: usize = 8;
pub const _POSIX_OPEN_MAX: usize = 20;
pub const _POSIX_FD_SETSIZE: usize = 20;
pub const _POSIX_OPEN_MAX: ::c_int = 20;
pub const _POSIX_FD_SETSIZE: ::c_int = 20;
pub const _POSIX_PATH_MAX: usize = 256;
pub const _POSIX_PIPE_BUF: usize = 512;
pub const _POSIX_RE_DUP_MAX: usize = 255;
Expand Down
4 changes: 2 additions & 2 deletions src/unix/linux_like/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ s! {
}

pub struct fd_set {
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
}

pub struct tm {
Expand Down Expand Up @@ -1065,7 +1065,7 @@ pub const PATH_MAX: ::c_int = 4096;

pub const UIO_MAXIOV: ::c_int = 1024;

pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

pub const EPOLLIN: u32 = 0x1;
pub const EPOLLPRI: u32 = 0x2;
Expand Down
8 changes: 4 additions & 4 deletions src/unix/newlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ s! {
}

pub struct fd_set { // Unverified
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
}

pub struct passwd { // Unverified
Expand Down Expand Up @@ -280,11 +280,11 @@ pub const PTHREAD_MUTEX_ERRORCHECK: ::c_int = 2;

cfg_if! {
if #[cfg(any(target_os = "horizon", target_os = "espidf"))] {
pub const FD_SETSIZE: usize = 64;
pub const FD_SETSIZE: ::c_int = 64;
} else if #[cfg(target_os = "vita")] {
pub const FD_SETSIZE: usize = 256;
pub const FD_SETSIZE: ::c_int = 256;
} else {
pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;
}
}
// intentionally not public, only used for fd_set
Expand Down
4 changes: 2 additions & 2 deletions src/unix/nto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ s! {
}

pub struct fd_set {
fds_bits: [::c_uint; 2 * FD_SETSIZE / ULONG_SIZE],
fds_bits: [::c_uint; 2 * FD_SETSIZE as usize / ULONG_SIZE],
}

pub struct tm {
Expand Down Expand Up @@ -1383,7 +1383,7 @@ pub const PATH_MAX: ::c_int = 1024;

pub const UIO_MAXIOV: ::c_int = 1024;

pub const FD_SETSIZE: usize = 256;
pub const FD_SETSIZE: ::c_int = 256;

pub const TCIOFF: ::c_int = 0x0002;
pub const TCION: ::c_int = 0x0003;
Expand Down
4 changes: 2 additions & 2 deletions src/unix/redox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ s! {
}

pub struct fd_set {
fds_bits: [::c_ulong; ::FD_SETSIZE / ULONG_SIZE],
fds_bits: [::c_ulong; ::FD_SETSIZE as usize / ULONG_SIZE],
}

pub struct in_addr {
Expand Down Expand Up @@ -759,7 +759,7 @@ pub const MS_INVALIDATE: ::c_int = 0x0002;
pub const MS_SYNC: ::c_int = 0x0004;

// sys/select.h
pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;

// sys/socket.h
pub const AF_INET: ::c_int = 2;
Expand Down
8 changes: 4 additions & 4 deletions src/unix/solarish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,9 @@ s_no_extra_traits! {

pub struct fd_set {
#[cfg(target_pointer_width = "64")]
fds_bits: [i64; FD_SETSIZE / 64],
fds_bits: [i64; FD_SETSIZE as usize / 64],
#[cfg(target_pointer_width = "32")]
fds_bits: [i32; FD_SETSIZE / 32],
fds_bits: [i32; FD_SETSIZE as usize / 32],
}

pub struct sockaddr_storage {
Expand Down Expand Up @@ -1236,9 +1236,9 @@ pub const IPV6_V6ONLY: ::c_int = 0x27;

cfg_if! {
if #[cfg(target_pointer_width = "64")] {
pub const FD_SETSIZE: usize = 65536;
pub const FD_SETSIZE: ::c_int = 65536;
} else {
pub const FD_SETSIZE: usize = 1024;
pub const FD_SETSIZE: ::c_int = 1024;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub const F_SETFD: c_int = 2;
pub const F_GETFL: c_int = 3;
pub const F_SETFL: c_int = 4;
pub const FD_CLOEXEC: c_int = 1;
pub const FD_SETSIZE: size_t = 1024;
pub const FD_SETSIZE: c_int = 1024;
pub const O_APPEND: c_int = 0x0001;
pub const O_DSYNC: c_int = 0x0002;
pub const O_NONBLOCK: c_int = 0x0004;
Expand Down

0 comments on commit 7412a8b

Please sign in to comment.