Skip to content

Commit 7412a8b

Browse files
committed
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.
2 parents a0d9be1 + 500365e commit 7412a8b

File tree

15 files changed

+29
-29
lines changed

15 files changed

+29
-29
lines changed

src/fuchsia/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ s! {
380380
}
381381

382382
pub struct fd_set {
383-
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
383+
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
384384
}
385385

386386
pub struct tm {
@@ -1827,7 +1827,7 @@ pub const SS_DISABLE: ::c_int = 2;
18271827

18281828
pub const PATH_MAX: ::c_int = 4096;
18291829

1830-
pub const FD_SETSIZE: usize = 1024;
1830+
pub const FD_SETSIZE: ::c_int = 1024;
18311831

18321832
pub const EPOLLIN: ::c_int = 0x1;
18331833
pub const EPOLLPRI: ::c_int = 0x2;

src/unix/aix/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2118,7 +2118,7 @@ pub const POWER_8: ::c_int = 0x10000;
21182118
pub const POWER_9: ::c_int = 0x20000;
21192119

21202120
// sys/time.h
2121-
pub const FD_SETSIZE: usize = 65534;
2121+
pub const FD_SETSIZE: ::c_int = 65534;
21222122
pub const TIMEOFDAY: ::c_int = 9;
21232123
pub const CLOCK_REALTIME: ::clockid_t = TIMEOFDAY as clockid_t;
21242124
pub const CLOCK_MONOTONIC: ::clockid_t = 10;

src/unix/bsd/apple/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4384,7 +4384,7 @@ pub const OS_SIGNPOST_INTERVAL_END: ::os_signpost_type_t = 0x02;
43844384
pub const MINSIGSTKSZ: ::size_t = 32768;
43854385
pub const SIGSTKSZ: ::size_t = 131072;
43864386

4387-
pub const FD_SETSIZE: usize = 1024;
4387+
pub const FD_SETSIZE: ::c_int = 1024;
43884388

43894389
pub const ST_NOSUID: ::c_ulong = 2;
43904390

src/unix/bsd/freebsdlike/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,7 @@ pub const SCHED_FIFO: ::c_int = 1;
11931193
pub const SCHED_OTHER: ::c_int = 2;
11941194
pub const SCHED_RR: ::c_int = 3;
11951195

1196-
pub const FD_SETSIZE: usize = 1024;
1196+
pub const FD_SETSIZE: ::c_int = 1024;
11971197

11981198
pub const ST_NOSUID: ::c_ulong = 2;
11991199

src/unix/bsd/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ s! {
5959
pub struct fd_set {
6060
#[cfg(all(target_pointer_width = "64",
6161
any(target_os = "freebsd", target_os = "dragonfly")))]
62-
fds_bits: [i64; FD_SETSIZE / 64],
62+
fds_bits: [i64; FD_SETSIZE as usize / 64],
6363
#[cfg(not(all(target_pointer_width = "64",
6464
any(target_os = "freebsd", target_os = "dragonfly"))))]
65-
fds_bits: [i32; FD_SETSIZE / 32],
65+
fds_bits: [i32; FD_SETSIZE as usize / 32],
6666
}
6767

6868
pub struct tm {

src/unix/bsd/netbsdlike/netbsd/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ pub const _SC_SCHED_RT_TS: ::c_int = 2001;
18471847
pub const _SC_SCHED_PRI_MIN: ::c_int = 2002;
18481848
pub const _SC_SCHED_PRI_MAX: ::c_int = 2003;
18491849

1850-
pub const FD_SETSIZE: usize = 0x100;
1850+
pub const FD_SETSIZE: ::c_int = 0x100;
18511851

18521852
pub const ST_NOSUID: ::c_ulong = 8;
18531853

src/unix/bsd/netbsdlike/openbsd/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1422,7 +1422,7 @@ pub const _SC_AVPHYS_PAGES: ::c_int = 501;
14221422
pub const _SC_NPROCESSORS_CONF: ::c_int = 502;
14231423
pub const _SC_NPROCESSORS_ONLN: ::c_int = 503;
14241424

1425-
pub const FD_SETSIZE: usize = 1024;
1425+
pub const FD_SETSIZE: ::c_int = 1024;
14261426

14271427
pub const SCHED_FIFO: ::c_int = 1;
14281428
pub const SCHED_OTHER: ::c_int = 2;

src/unix/haiku/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ pub const SA_ONESHOT: ::c_int = SA_RESETHAND;
11001100
pub const SS_ONSTACK: ::c_int = 0x1;
11011101
pub const SS_DISABLE: ::c_int = 0x2;
11021102

1103-
pub const FD_SETSIZE: usize = 1024;
1103+
pub const FD_SETSIZE: ::c_int = 1024;
11041104

11051105
pub const RTLD_LOCAL: ::c_int = 0x0;
11061106
pub const RTLD_NOW: ::c_int = 0x1;

src/unix/hurd/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ pub const SHM_UNLOCK: ::c_int = 12;
12111211
pub const STDIN_FILENO: ::c_int = 0;
12121212
pub const STDOUT_FILENO: ::c_int = 1;
12131213
pub const STDERR_FILENO: ::c_int = 2;
1214-
pub const __FD_SETSIZE: usize = 256;
1214+
pub const __FD_SETSIZE: ::c_int = 256;
12151215
pub const R_OK: ::c_int = 4;
12161216
pub const W_OK: ::c_int = 2;
12171217
pub const X_OK: ::c_int = 1;
@@ -1256,7 +1256,7 @@ pub const PDP_ENDIAN: usize = 3412;
12561256
pub const BYTE_ORDER: usize = 1234;
12571257

12581258
// sys/select.h
1259-
pub const FD_SETSIZE: usize = 256;
1259+
pub const FD_SETSIZE: ::c_int = 256;
12601260
pub const __SIZEOF_PTHREAD_MUTEX_T: usize = 32;
12611261
pub const __SIZEOF_PTHREAD_ATTR_T: usize = 32;
12621262
pub const __SIZEOF_PTHREAD_RWLOCK_T: usize = 28;
@@ -1588,8 +1588,8 @@ pub const _POSIX_MQ_OPEN_MAX: usize = 8;
15881588
pub const _POSIX_MQ_PRIO_MAX: usize = 32;
15891589
pub const _POSIX_NAME_MAX: usize = 14;
15901590
pub const _POSIX_NGROUPS_MAX: usize = 8;
1591-
pub const _POSIX_OPEN_MAX: usize = 20;
1592-
pub const _POSIX_FD_SETSIZE: usize = 20;
1591+
pub const _POSIX_OPEN_MAX: ::c_int = 20;
1592+
pub const _POSIX_FD_SETSIZE: ::c_int = 20;
15931593
pub const _POSIX_PATH_MAX: usize = 256;
15941594
pub const _POSIX_PIPE_BUF: usize = 512;
15951595
pub const _POSIX_RE_DUP_MAX: usize = 255;

src/unix/linux_like/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ s! {
8585
}
8686

8787
pub struct fd_set {
88-
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
88+
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
8989
}
9090

9191
pub struct tm {
@@ -1065,7 +1065,7 @@ pub const PATH_MAX: ::c_int = 4096;
10651065

10661066
pub const UIO_MAXIOV: ::c_int = 1024;
10671067

1068-
pub const FD_SETSIZE: usize = 1024;
1068+
pub const FD_SETSIZE: ::c_int = 1024;
10691069

10701070
pub const EPOLLIN: u32 = 0x1;
10711071
pub const EPOLLPRI: u32 = 0x2;

src/unix/newlib/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ s! {
171171
}
172172

173173
pub struct fd_set { // Unverified
174-
fds_bits: [::c_ulong; FD_SETSIZE / ULONG_SIZE],
174+
fds_bits: [::c_ulong; FD_SETSIZE as usize / ULONG_SIZE],
175175
}
176176

177177
pub struct passwd { // Unverified
@@ -280,11 +280,11 @@ pub const PTHREAD_MUTEX_ERRORCHECK: ::c_int = 2;
280280

281281
cfg_if! {
282282
if #[cfg(any(target_os = "horizon", target_os = "espidf"))] {
283-
pub const FD_SETSIZE: usize = 64;
283+
pub const FD_SETSIZE: ::c_int = 64;
284284
} else if #[cfg(target_os = "vita")] {
285-
pub const FD_SETSIZE: usize = 256;
285+
pub const FD_SETSIZE: ::c_int = 256;
286286
} else {
287-
pub const FD_SETSIZE: usize = 1024;
287+
pub const FD_SETSIZE: ::c_int = 1024;
288288
}
289289
}
290290
// intentionally not public, only used for fd_set

src/unix/nto/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ s! {
154154
}
155155

156156
pub struct fd_set {
157-
fds_bits: [::c_uint; 2 * FD_SETSIZE / ULONG_SIZE],
157+
fds_bits: [::c_uint; 2 * FD_SETSIZE as usize / ULONG_SIZE],
158158
}
159159

160160
pub struct tm {
@@ -1383,7 +1383,7 @@ pub const PATH_MAX: ::c_int = 1024;
13831383

13841384
pub const UIO_MAXIOV: ::c_int = 1024;
13851385

1386-
pub const FD_SETSIZE: usize = 256;
1386+
pub const FD_SETSIZE: ::c_int = 256;
13871387

13881388
pub const TCIOFF: ::c_int = 0x0002;
13891389
pub const TCION: ::c_int = 0x0003;

src/unix/redox/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ s! {
116116
}
117117

118118
pub struct fd_set {
119-
fds_bits: [::c_ulong; ::FD_SETSIZE / ULONG_SIZE],
119+
fds_bits: [::c_ulong; ::FD_SETSIZE as usize / ULONG_SIZE],
120120
}
121121

122122
pub struct in_addr {
@@ -759,7 +759,7 @@ pub const MS_INVALIDATE: ::c_int = 0x0002;
759759
pub const MS_SYNC: ::c_int = 0x0004;
760760

761761
// sys/select.h
762-
pub const FD_SETSIZE: usize = 1024;
762+
pub const FD_SETSIZE: ::c_int = 1024;
763763

764764
// sys/socket.h
765765
pub const AF_INET: ::c_int = 2;

src/unix/solarish/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,9 @@ s_no_extra_traits! {
514514

515515
pub struct fd_set {
516516
#[cfg(target_pointer_width = "64")]
517-
fds_bits: [i64; FD_SETSIZE / 64],
517+
fds_bits: [i64; FD_SETSIZE as usize / 64],
518518
#[cfg(target_pointer_width = "32")]
519-
fds_bits: [i32; FD_SETSIZE / 32],
519+
fds_bits: [i32; FD_SETSIZE as usize / 32],
520520
}
521521

522522
pub struct sockaddr_storage {
@@ -1236,9 +1236,9 @@ pub const IPV6_V6ONLY: ::c_int = 0x27;
12361236

12371237
cfg_if! {
12381238
if #[cfg(target_pointer_width = "64")] {
1239-
pub const FD_SETSIZE: usize = 65536;
1239+
pub const FD_SETSIZE: ::c_int = 65536;
12401240
} else {
1241-
pub const FD_SETSIZE: usize = 1024;
1241+
pub const FD_SETSIZE: ::c_int = 1024;
12421242
}
12431243
}
12441244

src/wasi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub const F_SETFD: c_int = 2;
207207
pub const F_GETFL: c_int = 3;
208208
pub const F_SETFL: c_int = 4;
209209
pub const FD_CLOEXEC: c_int = 1;
210-
pub const FD_SETSIZE: size_t = 1024;
210+
pub const FD_SETSIZE: c_int = 1024;
211211
pub const O_APPEND: c_int = 0x0001;
212212
pub const O_DSYNC: c_int = 0x0002;
213213
pub const O_NONBLOCK: c_int = 0x0004;

0 commit comments

Comments
 (0)