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

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
parent 9621f8a9ac
commit 0989529f88
2 changed files with 19 additions and 15 deletions

View file

@ -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?)

33
time.c
View file

@ -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) {