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

ruby-changes:54200

From: nobu <ko1@a...>
Date: Mon, 17 Dec 2018 09:43:47 +0900 (JST)
Subject: [ruby-changes:54200] nobu:r66421 (trunk): Range check is only for interaval

nobu	2018-12-17 09:43:40 +0900 (Mon, 17 Dec 2018)

  New Revision: 66421

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66421

  Log:
    Range check is only for interaval
    
    * time.c (time_timespec): range check is only for time interval
      value if time_t is signed.

  Modified files:
    trunk/time.c
Index: time.c
===================================================================
--- time.c	(revision 66420)
+++ time.c	(revision 66421)
@@ -2518,22 +2518,29 @@ time_timespec(VALUE num, int interval) https://github.com/ruby/ruby/blob/trunk/time.c#L2518
     VALUE i, f, ary;
 
 #ifndef NEGATIVE_TIME_T
-    interval = 1;
+# define arg_range_check(v) \
+    (((v) < 0) ? \
+     rb_raise(rb_eArgError, "%s must not be negative", tstr) : \
+     (void)0)
+#else
+# define arg_range_check(v) \
+    ((interval && (v) < 0) ? \
+     rb_raise(rb_eArgError, "time interval must not be negative") : \
+     (void)0)
 #endif
 
     if (FIXNUM_P(num)) {
 	t.tv_sec = NUM2TIMET(num);
-	if (interval && t.tv_sec < 0)
-            rb_raise(rb_eArgError, "%s must not be negative", tstr);
+        arg_range_check(t.tv_sec);
 	t.tv_nsec = 0;
     }
     else if (RB_FLOAT_TYPE_P(num)) {
-	if (interval && RFLOAT_VALUE(num) < 0.0)
-            rb_raise(rb_eArgError, "%s must not be negative", tstr);
-	else {
+        double x = RFLOAT_VALUE(num);
+        arg_range_check(x);
+        {
 	    double f, d;
 
-	    d = modf(RFLOAT_VALUE(num), &f);
+            d = modf(x, &f);
 	    if (d >= 0) {
 		t.tv_nsec = (int)(d*1e9+0.5);
 		if (t.tv_nsec >= 1000000000) {
@@ -2547,14 +2554,13 @@ time_timespec(VALUE num, int interval) https://github.com/ruby/ruby/blob/trunk/time.c#L2554
 	    }
 	    t.tv_sec = (time_t)f;
 	    if (f != t.tv_sec) {
-		rb_raise(rb_eRangeError, "%f out of Time range", RFLOAT_VALUE(num));
+                rb_raise(rb_eRangeError, "%f out of Time range", x);
 	    }
 	}
     }
     else if (RB_TYPE_P(num, T_BIGNUM)) {
 	t.tv_sec = NUM2TIMET(num);
-	if (interval && t.tv_sec < 0)
-            rb_raise(rb_eArgError, "%s must not be negative", tstr);
+        arg_range_check(t.tv_sec);
 	t.tv_nsec = 0;
     }
     else {
@@ -2564,8 +2570,7 @@ time_timespec(VALUE num, int interval) https://github.com/ruby/ruby/blob/trunk/time.c#L2570
             i = rb_ary_entry(ary, 0);
             f = rb_ary_entry(ary, 1);
             t.tv_sec = NUM2TIMET(i);
-            if (interval && t.tv_sec < 0)
-                rb_raise(rb_eArgError, "%s must not be negative", tstr);
+            arg_range_check(t.tv_sec);
             f = rb_funcall(f, '*', 1, INT2FIX(1000000000));
             t.tv_nsec = NUM2LONG(f);
         }
@@ -2575,6 +2580,7 @@ time_timespec(VALUE num, int interval) https://github.com/ruby/ruby/blob/trunk/time.c#L2580
         }
     }
     return t;
+#undef arg_range_check
 }
 
 static struct timeval

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

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