diff options
author | ache <ache@FreeBSD.org> | 1997-10-17 09:35:50 +0000 |
---|---|---|
committer | ache <ache@FreeBSD.org> | 1997-10-17 09:35:50 +0000 |
commit | 47a375370a838aa4b2c79ca89acd4c9eb92ac08a (patch) | |
tree | 3d954c763532297c4803026a3dc81d2519c1751d /lib/libc | |
parent | ff34791ad2eb672cf0cae36791aa40d7a6ce42b2 (diff) | |
download | FreeBSD-src-47a375370a838aa4b2c79ca89acd4c9eb92ac08a.zip FreeBSD-src-47a375370a838aa4b2c79ca89acd4c9eb92ac08a.tar.gz |
Fix LONG_MAX overflowing
Return seconds if errno other than EINTR
Add $Id
Submitted by: bde with minor optimization by me
Diffstat (limited to 'lib/libc')
-rw-r--r-- | lib/libc/gen/sleep.c | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/lib/libc/gen/sleep.c b/lib/libc/gen/sleep.c index c68aa3d..5789647 100644 --- a/lib/libc/gen/sleep.c +++ b/lib/libc/gen/sleep.c @@ -32,9 +32,15 @@ */ #if defined(LIBC_SCCS) && !defined(lint) +#if 0 static char sccsid[] = "@(#)sleep.c 8.1 (Berkeley) 6/4/93"; +#endif +static char rcsid[] = + "$Id$"; #endif /* LIBC_SCCS and not lint */ +#include <errno.h> +#include <limits.h> #include <time.h> #include <unistd.h> @@ -45,14 +51,19 @@ sleep(seconds) struct timespec time_to_sleep; struct timespec time_remaining; - if (seconds != 0) { - time_to_sleep.tv_sec = seconds; - time_to_sleep.tv_nsec = 0; - time_remaining = time_to_sleep; - (void)nanosleep(&time_to_sleep, &time_remaining); - seconds = time_remaining.tv_sec; - if (time_remaining.tv_nsec > 0) - seconds++; /* round up */ - } - return (seconds); + /* + * Avoid overflow when `seconds' is huge. This assumes that + * the maximum value for a time_t is >= LONG_MAX. + */ + if (seconds > LONG_MAX) + return (seconds - LONG_MAX + sleep(LONG_MAX)); + + time_to_sleep.tv_sec = seconds; + time_to_sleep.tv_nsec = 0; + if (nanosleep(&time_to_sleep, &time_remaining) != -1) + return (0); + if (errno != EINTR) + return (seconds); /* best guess */ + return (time_remaining.tv_sec + + (time_remaining.tv_nsec != 0)); /* round up */ } |