You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto merge of #3356 - magicant:fd_setsize_type, r=JohnTitor
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.
0 commit comments