From 9e39bc1a088e0ac8cb6433c4daab03136253bf9d Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 17 Apr 2013 01:58:25 +0000 Subject: thread.c: fix overflow on Windows * thread.c (sleep_timeval): get rid of overflow on Windows where timeval.tv_sec is not time_t but mere long. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40329 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ thread.c | 23 ++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index f1abd7a05d..ebcd95c46b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Wed Apr 17 10:58:18 2013 Nobuyoshi Nakada + + * thread.c (sleep_timeval): get rid of overflow on Windows where + timeval.tv_sec is not time_t but mere long. + Tue Apr 16 23:07:12 2013 Tanaka Akira * ext/socket/unixsocket.c (unix_send_io): Suppress a warning by clang. diff --git a/thread.c b/thread.c index 8a1e090160..280362d690 100644 --- a/thread.c +++ b/thread.c @@ -912,18 +912,19 @@ thread_value(VALUE self) * Thread Scheduling */ +#if SIGNEDNESS_OF_TIME_T < 0 /* signed */ +# define TIMEVAL_SEC_MAX_P1 (((unsigned_time_t)1) << (sizeof(TYPEOF_TIMEVAL_TV_SEC) * CHAR_BIT - 1)) +# define TIMEVAL_SEC_MAX ((TYPEOF_TIMEVAL_TV_SEC)(TIMEVAL_SEC_MAX_P1 - 1)) +# define TIMEVAL_SEC_MIN ((TYPEOF_TIMEVAL_TV_SEC)TIMEVAL_SEC_MAX_P1) +#elif SIGNEDNESS_OF_TIME_T > 0 /* unsigned */ +# define TIMEVAL_SEC_MAX ((TYPEOF_TIMEVAL_TV_SEC)(~(unsigned_time_t)0)) +# define TIMEVAL_SEC_MIN ((TYPEOF_TIMEVAL_TV_SEC)0) +#endif + static struct timeval double2timeval(double d) { /* assume timeval.tv_sec has same signedness as time_t */ -#if SIGNEDNESS_OF_TIME_T < 0 /* signed */ - const unsigned_time_t TIMEVAL_SEC_MAX_P1 = (((unsigned_time_t)1) << (sizeof(TYPEOF_TIMEVAL_TV_SEC) * CHAR_BIT - 1)); - const TYPEOF_TIMEVAL_TV_SEC TIMEVAL_SEC_MAX = (TYPEOF_TIMEVAL_TV_SEC)(TIMEVAL_SEC_MAX_P1 - 1); - const TYPEOF_TIMEVAL_TV_SEC TIMEVAL_SEC_MIN = (TYPEOF_TIMEVAL_TV_SEC)TIMEVAL_SEC_MAX_P1; -#elif SIGNEDNESS_OF_TIME_T > 0 /* unsigned */ - const TYPEOF_TIMEVAL_TV_SEC TIMEVAL_SEC_MAX = (TYPEOF_TIMEVAL_TV_SEC)(~(unsigned_time_t)0); - const TYPEOF_TIMEVAL_TV_SEC TIMEVAL_SEC_MIN = (TYPEOF_TIMEVAL_TV_SEC)0; -#endif const double TIMEVAL_SEC_MAX_PLUS_ONE = (2*(double)(TIMEVAL_SEC_MAX/2+1)); struct timeval time; @@ -994,12 +995,12 @@ sleep_timeval(rb_thread_t *th, struct timeval tv, int spurious_check) enum rb_thread_status prev_status = th->status; getclockofday(&to); - if (TIMET_MAX - tv.tv_sec < to.tv_sec) - to.tv_sec = TIMET_MAX; + if (TIMEVAL_SEC_MAX - tv.tv_sec < to.tv_sec) + to.tv_sec = TIMEVAL_SEC_MAX; else to.tv_sec += tv.tv_sec; if ((to.tv_usec += tv.tv_usec) >= 1000000) { - if (to.tv_sec == TIMET_MAX) + if (to.tv_sec == TIMEVAL_SEC_MAX) to.tv_usec = 999999; else { to.tv_sec++; -- cgit v1.2.1