aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAndrey A. Chernov <ache@FreeBSD.org>1997-10-17 09:35:50 +0000
committerAndrey A. Chernov <ache@FreeBSD.org>1997-10-17 09:35:50 +0000
commit8cfedef0e60b611e9f01975eb3108e9eca58c5f6 (patch)
treed952f235eba6085cf7996f9892ca645588ec6076 /lib
parent9daa8a94aef25720ab9b5fa0edc731a29545443f (diff)
downloadsrc-8cfedef0e60b611e9f01975eb3108e9eca58c5f6.tar.gz
src-8cfedef0e60b611e9f01975eb3108e9eca58c5f6.zip
Fix LONG_MAX overflowing
Return seconds if errno other than EINTR Add $Id Submitted by: bde with minor optimization by me
Notes
Notes: svn path=/head/; revision=30510
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/gen/sleep.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/lib/libc/gen/sleep.c b/lib/libc/gen/sleep.c
index c68aa3d9f689..5789647c413c 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 */
}