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
This commit is contained in:
nobu 2014-05-02 18:43:23 +00:00
Родитель 9621f8a9ac
Коммит 0989529f88
2 изменённых файлов: 19 добавлений и 15 удалений

Просмотреть файл

@ -484,6 +484,7 @@ class TestTime < Test::Unit::TestCase
t3 = t1.getlocal("-02:00") t3 = t1.getlocal("-02:00")
assert_equal(t1, t3) assert_equal(t1, t3)
assert_equal(-7200, t3.utc_offset) 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 t1.localtime
assert_equal(t1, t2) assert_equal(t1, t2)
assert_equal(t1.gmt?, t2.gmt?) assert_equal(t1.gmt?, t2.gmt?)

33
time.c
Просмотреть файл

@ -1997,37 +1997,40 @@ vtm_add_offset(struct vtm *vtm, VALUE off)
not_zero_sec: not_zero_sec:
/* If sec + subsec == 0, don't change vtm->sec. /* If sec + subsec == 0, don't change vtm->sec.
* It may be 60 which is a leap second. */ * It may be 60 which is a leap second. */
vtm->sec += sec; sec += vtm->sec;
if (vtm->sec < 0) { if (sec < 0) {
vtm->sec += 60; sec += 60;
min -= 1; min -= 1;
} }
if (60 <= vtm->sec) { if (60 <= sec) {
vtm->sec -= 60; sec -= 60;
min += 1; min += 1;
} }
vtm->sec = sec;
} }
if (min) { if (min) {
vtm->min += min; min += vtm->min;
if (vtm->min < 0) { if (min < 0) {
vtm->min += 60; min += 60;
hour -= 1; hour -= 1;
} }
if (60 <= vtm->min) { if (60 <= min) {
vtm->min -= 60; min -= 60;
hour += 1; hour += 1;
} }
vtm->min = min;
} }
if (hour) { if (hour) {
vtm->hour += hour; hour += vtm->hour;
if (vtm->hour < 0) { if (hour < 0) {
vtm->hour += 24; hour += 24;
day = -1; day = -1;
} }
if (24 <= vtm->hour) { if (24 <= hour) {
vtm->hour -= 24; hour -= 24;
day = 1; day = 1;
} }
vtm->hour = hour;
} }
if (day) { if (day) {