From fef170e87c7e49db5f6c6eab7bcd1b12697c7e7f Mon Sep 17 00:00:00 2001 From: akr Date: Thu, 8 Aug 2013 12:59:21 +0000 Subject: * 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 --- time.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'time.c') 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; -- cgit v1.2.3