1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2013-04-17 01:58:25 +00:00
parent 08f52b05b2
commit 9e39bc1a08
2 changed files with 17 additions and 11 deletions

View file

@ -1,3 +1,8 @@
Wed Apr 17 10:58:18 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <akr@fsij.org>
* ext/socket/unixsocket.c (unix_send_io): Suppress a warning by clang.

View file

@ -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++;