Skip to content

Commit b3cd8a1

Browse files
authored
Use a consistent error mechanism for timeout overflow. (#1422)
Make `select` and `kqueue::kevent` fail with `Errno::INVAL` when converting a timeout value overflows, for consistency with the other polling APIs.
1 parent 10ce1e0 commit b3cd8a1

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/backend/libc/event/syscalls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub(crate) unsafe fn select(
229229
Some(timeout) => {
230230
// Convert from `Timespec` to `c::timeval`.
231231
timeout_data = c::timeval {
232-
tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
232+
tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?,
233233
tv_usec: ((timeout.tv_nsec + 999) / 1000) as _,
234234
};
235235
&timeout_data
@@ -306,7 +306,7 @@ pub(crate) unsafe fn select(
306306
Some(timeout) => {
307307
// Convert from `Timespec` to `c::timeval`.
308308
timeout_data = c::timeval {
309-
tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::OVERFLOW)?,
309+
tv_sec: timeout.tv_sec.try_into().map_err(|_| io::Errno::INVAL)?,
310310
tv_usec: ((timeout.tv_nsec + 999) / 1000) as _,
311311
};
312312
&timeout_data

src/event/kqueue.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ pub fn kqueue() -> io::Result<OwnedFd> {
398398
/// `kevent(kqueue, changelist, eventlist, timeout)`—Wait for events on a
399399
/// `kqueue`.
400400
///
401+
/// If an unsupported timeout is passed, this function fails with
402+
/// [`io::Errno::INVAL`].
403+
///
401404
/// # Safety
402405
///
403406
/// The file descriptors referred to by the `Event` structs must be valid for
@@ -421,10 +424,13 @@ pub unsafe fn kevent<Fd: AsFd, Buf: Buffer<Event>>(
421424
mut eventlist: Buf,
422425
timeout: Option<Duration>,
423426
) -> io::Result<Buf::Output> {
424-
let timeout = timeout.map(|timeout| backend::c::timespec {
425-
tv_sec: timeout.as_secs() as _,
426-
tv_nsec: timeout.subsec_nanos() as _,
427-
});
427+
let timeout = match timeout {
428+
Some(timeout) => Some(backend::c::timespec {
429+
tv_sec: timeout.as_secs().try_into().map_err(|_| io::Errno::INVAL)?,
430+
tv_nsec: timeout.subsec_nanos() as _,
431+
}),
432+
None => None,
433+
};
428434

429435
// Populate the event list with events.
430436
let len = syscalls::kevent(

src/event/port.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ pub unsafe fn dissociate_fd<Fd: AsFd, RawFd: AsRawFd>(port: Fd, object: RawFd) -
123123

124124
/// `port_get(port, timeout)`—Gets an event from a port.
125125
///
126+
/// If an unsupported timeout is passed, this function fails with
127+
/// [`io::Errno::INVAL`].
128+
///
126129
/// # References
127130
/// - [OpenSolaris]
128131
/// - [illumos]
@@ -141,6 +144,9 @@ pub fn get<Fd: AsFd>(port: Fd, timeout: Option<&Timespec>) -> io::Result<Event>
141144
///
142145
/// To query the number of events without retrieving any, use [`getn_query`].
143146
///
147+
/// If an unsupported timeout is passed, this function fails with
148+
/// [`io::Errno::INVAL`].
149+
///
144150
/// # References
145151
/// - [OpenSolaris]
146152
/// - [illumos]

src/event/select.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ pub struct FdSetElement(pub(crate) usize);
6969
/// `readfds`, `writefds`, `exceptfds` must point to arrays of `FdSetElement`
7070
/// containing at least `nfds.div_ceil(size_of::<FdSetElement>())` elements.
7171
///
72+
/// If an unsupported timeout is passed, this function fails with
73+
/// [`io::Errno::INVAL`].
74+
///
7275
/// This `select` wrapper differs from POSIX in that `nfds` is not limited to
7376
/// `FD_SETSIZE`. Instead of using the fixed-sized `fd_set` type, this function
7477
/// takes raw pointers to arrays of `fd_set_num_elements(max_fd + 1, num_fds)`,

0 commit comments

Comments
 (0)