From acc3c3085531d410e9a0a0acb443b656fcb81067 Mon Sep 17 00:00:00 2001 From: Erik van der Kouwe Date: Thu, 7 Jan 2010 19:25:18 +0000 Subject: [PATCH] Prevent nanosleep from potentially overwriting sleep time suggested by Rene Zatvo --- lib/posix/_nanosleep.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/posix/_nanosleep.c b/lib/posix/_nanosleep.c index d926f5056..18319e67d 100644 --- a/lib/posix/_nanosleep.c +++ b/lib/posix/_nanosleep.c @@ -22,6 +22,7 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { struct timeval timeout, timestart = { 0, 0 }, timeend; int errno_select, r; + struct timespec rqt; /* check parameters */ if (!rqtp) @@ -32,6 +33,9 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) rqtp->tv_nsec >= NSEC_PER_SEC) return EINVAL; + /* store *rqtp to make sure it is not overwritten */ + rqt = *rqtp; + /* keep track of start time if needed */ if (rmtp) { @@ -42,8 +46,8 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) } /* use select to wait */ - timeout.tv_sec = rqtp->tv_sec; - timeout.tv_usec = (rqtp->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC; + timeout.tv_sec = rqt.tv_sec; + timeout.tv_usec = (rqt.tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC; r = select(0, NULL, NULL, NULL, &timeout); /* return remaining time only if requested */ @@ -59,8 +63,8 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) errno = errno_select; /* compute remaining time */ - rmtp->tv_sec = rqtp->tv_sec - (timeend.tv_sec - timestart.tv_sec); - rmtp->tv_nsec = rqtp->tv_nsec - (timeend.tv_usec - timestart.tv_usec) * NSEC_PER_USEC; + rmtp->tv_sec = rqt.tv_sec - (timeend.tv_sec - timestart.tv_sec); + rmtp->tv_nsec = rqt.tv_nsec - (timeend.tv_usec - timestart.tv_usec) * NSEC_PER_USEC; /* bring remaining time into canonical form */ while (rmtp->tv_nsec < 0) -- 2.44.0