[前][次][番号順一覧][スレッド一覧]

ruby-changes:33711

From: nobu <ko1@a...>
Date: Sat, 3 May 2014 03:43:32 +0900 (JST)
Subject: [ruby-changes:33711] nobu:r45792 (trunk): time.c: fix underflow of unsigned integers

nobu	2014-05-03 03:43:23 +0900 (Sat, 03 May 2014)

  New Revision: 45792

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45792

  Log:
    time.c: fix underflow of unsigned integers
    
    * time.c (vtm_add_offset): get rid of underflow of unsigned
      integers.  fix up r45155.

  Modified files:
    trunk/test/ruby/test_time.rb
    trunk/time.c
Index: time.c
===================================================================
--- time.c	(revision 45791)
+++ time.c	(revision 45792)
@@ -1997,37 +1997,40 @@ vtm_add_offset(struct vtm *vtm, VALUE of https://github.com/ruby/ruby/blob/trunk/time.c#L1997
       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) {
Index: test/ruby/test_time.rb
===================================================================
--- test/ruby/test_time.rb	(revision 45791)
+++ test/ruby/test_time.rb	(revision 45792)
@@ -484,6 +484,7 @@ class TestTime < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_time.rb#L484
     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?)

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]