From 0989529f88efc0882fd5f567350d196664bf8318 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 2 May 2014 18:43:23 +0000 Subject: [PATCH] time.c: fix underflow of unsigned integers * time.c (vtm_add_offset): get rid of underflow of unsigned integers. fix up r45155. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45792 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_time.rb | 1 + time.c | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index 3935905d04..cdce37f796 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -484,6 +484,7 @@ class TestTime < Test::Unit::TestCase t3 = t1.getlocal("-02:00") assert_equal(t1, t3) assert_equal(-7200, t3.utc_offset) + assert_equal([1999, 12, 31, 22, 0, 0], [t3.year, t3.mon, t3.mday, t3.hour, t3.min, t3.sec]) t1.localtime assert_equal(t1, t2) assert_equal(t1.gmt?, t2.gmt?) diff --git a/time.c b/time.c index fcf5717255..43ae533829 100644 --- a/time.c +++ b/time.c @@ -1997,37 +1997,40 @@ vtm_add_offset(struct vtm *vtm, VALUE off) not_zero_sec: /* If sec + subsec == 0, don't change vtm->sec. * It may be 60 which is a leap second. */ - vtm->sec += sec; - if (vtm->sec < 0) { - vtm->sec += 60; + sec += vtm->sec; + if (sec < 0) { + sec += 60; min -= 1; } - if (60 <= vtm->sec) { - vtm->sec -= 60; + if (60 <= sec) { + sec -= 60; min += 1; } + vtm->sec = sec; } if (min) { - vtm->min += min; - if (vtm->min < 0) { - vtm->min += 60; + min += vtm->min; + if (min < 0) { + min += 60; hour -= 1; } - if (60 <= vtm->min) { - vtm->min -= 60; + if (60 <= min) { + min -= 60; hour += 1; } + vtm->min = min; } if (hour) { - vtm->hour += hour; - if (vtm->hour < 0) { - vtm->hour += 24; + hour += vtm->hour; + if (hour < 0) { + hour += 24; day = -1; } - if (24 <= vtm->hour) { - vtm->hour -= 24; + if (24 <= hour) { + hour -= 24; day = 1; } + vtm->hour = hour; } if (day) {