From fef170e87c7e49db5f6c6eab7bcd1b12697c7e7f Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 8 Aug 2013 12:59:21 +0000 Subject: [PATCH] * time.c (time_overflow_p): Avoid signed integer overflow. (rb_time_new): Fix overflow condition. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42447 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 3 ++- time.c | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7f378a4e54..7e58e7a895 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,7 @@ -Thu Aug 8 21:32:22 2013 Tanaka Akira +Thu Aug 8 21:56:44 2013 Tanaka Akira * time.c (time_overflow_p): Avoid signed integer overflow. + (rb_time_new): Fix overflow condition. Thu Aug 8 19:58:02 2013 Koichi Sasada diff --git a/time.c b/time.c index 51d7581e01..20476e9343 100644 --- a/time.c +++ b/time.c @@ -2226,16 +2226,16 @@ time_overflow_p(time_t *secp, long *nsecp) if (TIMET_MAX - sec2 < sec) { rb_raise(rb_eRangeError, "out of Time range"); } - nsec %= 1000000000; + nsec -= sec2 * 1000000000; sec += sec2; } - if (nsec < 0) { /* nsec negative overflow */ + else if (nsec < 0) { /* nsec negative overflow */ sec2 = NDIV(nsec,1000000000); /* negative div */ if (sec < TIMET_MIN - sec2) { rb_raise(rb_eRangeError, "out of Time range"); } - nsec = NMOD(nsec,1000000000); /* negative mod */ - sec = sec + sec2; + nsec -= sec2 * 1000000000; + sec += sec2; } #ifndef NEGATIVE_TIME_T if (sec < 0) @@ -2281,9 +2281,9 @@ rb_time_new(time_t sec, long usec) usec -= sec2 * 1000000; sec += sec2; } - else if (usec <= 1000000) { - long sec2 = usec / 1000000; - if (sec < -TIMET_MAX - sec2) { + else if (usec < 0) { + long sec2 = NDIV(usec,1000000); /* negative div */ + if (sec < TIMET_MIN - sec2) { rb_raise(rb_eRangeError, "out of Time range"); } usec -= sec2 * 1000000;