diff options
| author | 2020-03-20 03:37:08 +0000 | |
|---|---|---|
| committer | 2020-03-20 03:37:08 +0000 | |
| commit | 375723add634b352ea99cdf4dff635a8389e8913 (patch) | |
| tree | dc0d0f7a08d1c282c1bdd4745e8f5844206c4dfa /sys/kern/kern_synch.c | |
| parent | Return when create_solver() returns NULL to prevent null pointer dereference (diff) | |
| download | wireguard-openbsd-375723add634b352ea99cdf4dff635a8389e8913.tar.xz wireguard-openbsd-375723add634b352ea99cdf4dff635a8389e8913.zip | |
tsleep_nsec(9): add MAXTSLP macro, the maximum sleep duration
This macro will be useful for truncating durations below INFSLP
(UINT64_MAX) when converting from a timespec or timeval to a count
of nanoseconds before calling tsleep_nsec(9), msleep_nsec(9), or
rwsleep_nsec(9).
A relative timespec can hold many more nanoseconds than a uint64_t
can. TIMESPEC_TO_NSEC() and TIMEVAL_TO_NSEC() check for overflow,
returning UINT64_MAX if the conversion would overflow a uint64_t.
Thus, MAXTSLP will make it easy to avoid inadvertently passing INFSLP
to tsleep_nsec(9) et al. when the caller intended to set a timeout.
The code in such a case might look something like this:
uint64_t nsecs = MIN(TIMESPEC_TO_NSEC(&ts), MAXTSLP);
The macro may also be useful for rejecting intervals that are "too large",
e.g. for sockets with timeouts, if the timeout duration is to be stored
as a uint64_t in an object in the kernel. The code in such a case might
look something like this:
case SIOCTIMEOUT:
{
struct timeval *tv = (struct timeval *)data;
uint64_t nsecs;
if (tv->tv_sec < 0 || !timerisvalid(tv))
return EINVAL;
nsecs = TIMEVAL_TO_NSEC(tv);
if (nsecs > MAXTSLP)
return EOVERFLOW;
obj.timeout = nsecs;
break;
}
Idea suggested by visa@.
ok visa@
Diffstat (limited to 'sys/kern/kern_synch.c')
0 files changed, 0 insertions, 0 deletions
