diff options
author | cheloha <cheloha@cvs.openbsd.org> | 2018-05-22 18:33:42 +0000 |
---|---|---|
committer | cheloha <cheloha@cvs.openbsd.org> | 2018-05-22 18:33:42 +0000 |
commit | edaa4fc85aa6a6f2a7a6371f2282ab83c49cec3c (patch) | |
tree | 50a6362302870ab54204c91758f8457f693f1546 /regress | |
parent | 0b4fa9bb3d4d2bbaafe1414f86abd2d5043a197b (diff) |
nanosleep: ensure tv_nsec input is on [0, 1000000000)
Instead of converting timespec -> timeval and truncating the input,
check with timespecfix and use tstohz(9) for the tsleep.
All other contemporary systems check this correctly.
Also add a regression test for this case.
ok tb@
Diffstat (limited to 'regress')
-rw-r--r-- | regress/sys/kern/nanosleep/Makefile | 7 | ||||
-rw-r--r-- | regress/sys/kern/nanosleep/nanosleep.c | 28 |
2 files changed, 30 insertions, 5 deletions
diff --git a/regress/sys/kern/nanosleep/Makefile b/regress/sys/kern/nanosleep/Makefile index fca0da5e67f..919e33e6769 100644 --- a/regress/sys/kern/nanosleep/Makefile +++ b/regress/sys/kern/nanosleep/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.3 2002/09/02 20:01:44 avsm Exp $ +# $OpenBSD: Makefile,v 1.4 2018/05/22 18:33:41 cheloha Exp $ PROG= nanosleep SRCS= nanosleep.c @@ -18,7 +18,10 @@ time_elapsed_with_signal: nanosleep short_time: nanosleep ./nanosleep -S +invalid_time: nanosleep + ./nanosleep -i + REGRESS_TARGETS=trivial with_signal time_elapsed time_elapsed_with_signal -REGRESS_TARGETS+=short_time +REGRESS_TARGETS+=short_time invalid_time .include <bsd.regress.mk> diff --git a/regress/sys/kern/nanosleep/nanosleep.c b/regress/sys/kern/nanosleep/nanosleep.c index fad3ec88459..5ccc0826235 100644 --- a/regress/sys/kern/nanosleep/nanosleep.c +++ b/regress/sys/kern/nanosleep/nanosleep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nanosleep.c,v 1.6 2017/03/08 19:28:47 deraadt Exp $ */ +/* $OpenBSD: nanosleep.c,v 1.7 2018/05/22 18:33:41 cheloha Exp $ */ /* * Written by Artur Grabowski <art@openbsd.org> 2002 Public Domain. */ @@ -6,6 +6,7 @@ #include <sys/time.h> #include <sys/wait.h> +#include <errno.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> @@ -13,6 +14,7 @@ #include <err.h> #include <signal.h> +int invalid_time(void); int trivial(void); int with_signal(void); int time_elapsed(void); @@ -29,8 +31,11 @@ main(int argc, char **argv) ret = 0; - while ((ch = getopt(argc, argv, "tseES")) != -1) { + while ((ch = getopt(argc, argv, "itseES")) != -1) { switch (ch) { + case 'i': + ret |= invalid_time(); + break; case 't': ret |= trivial(); break; @@ -46,7 +51,7 @@ main(int argc, char **argv) case 'S': ret |= short_time(); default: - fprintf(stderr, "Usage: nanosleep [-tse]\n"); + fprintf(stderr, "Usage: nanosleep [-itseSE]\n"); exit(1); } } @@ -259,3 +264,20 @@ short_time(void) return 0; } + +int +invalid_time(void) +{ + struct timespec ts[3] = { {-1, 0}, {0, -1}, {0, 1000000000L} }; + int i, status; + + for (i = 0; i < 3; i++) { + status = nanosleep(&ts[i], NULL); + if (status != -1 || errno != EINVAL) { + warnx("invalid-time: nanosleep %lld %ld", + (long long)ts[i].tv_sec, ts[i].tv_nsec); + return 1; + } + } + return 0; +} |