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

ruby-changes:33108

From: nobu <ko1@a...>
Date: Thu, 27 Feb 2014 12:10:17 +0900 (JST)
Subject: [ruby-changes:33108] nobu:r45187 (trunk): numeric.c: check signs before division

nobu	2014-02-27 12:10:12 +0900 (Thu, 27 Feb 2014)

  New Revision: 45187

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

  Log:
    numeric.c: check signs before division
    
    * numeric.c (ruby_num_interval_step_size): check signs and get rid
      of implementation dependent behavior of negative division.
      [ruby-core:61106] [Bug #9570]

  Modified files:
    trunk/ChangeLog
    trunk/numeric.c
    trunk/test/ruby/test_numeric.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45186)
+++ ChangeLog	(revision 45187)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Feb 27 12:10:09 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* numeric.c (ruby_num_interval_step_size): check signs and get rid
+	  of implementation dependent behavior of negative division.
+	  [ruby-core:61106] [Bug #9570]
+
 Thu Feb 27 03:55:45 2014  Zachary Scott  <e@z...>
 
 	* thread.c: [DOC] Typo in comment for _FORTIFY_SOURCE [Fixes GH-548]
Index: numeric.c
===================================================================
--- numeric.c	(revision 45186)
+++ numeric.c	(revision 45187)
@@ -1808,10 +1808,21 @@ ruby_num_interval_step_size(VALUE from, https://github.com/ruby/ruby/blob/trunk/numeric.c#L1808
 	long delta, diff, result;
 
 	diff = FIX2LONG(step);
+	if (!diff) rb_num_zerodiv();
 	delta = FIX2LONG(to) - FIX2LONG(from);
 	if (excl) {
 	    delta += (diff > 0 ? -1 : +1);
 	}
+	if (delta) {
+	    if (diff < 0) {
+		if (delta > 0) return INT2FIX(0);
+		diff = -diff;
+		delta = -delta;
+	    }
+	    else {
+		if (delta < 0) return INT2FIX(0);
+	    }
+	}
 	result = delta / diff;
 	return LONG2FIX(result >= 0 ? result + 1 : 0);
     }
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 45186)
+++ test/ruby/test_numeric.rb	(revision 45187)
@@ -279,6 +279,8 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L279
     assert_step [1], [1, 10, 2**32]
     assert_step [1], [1, to: 10, by: 2**32]
 
+    assert_step [], [2, 1, 3]
+    assert_step [], [-2, -1, -3]
     assert_step [3, 3, 3, 3], [3, by: 0], inf: true
     assert_step [10], [10, 1, -(2**32)]
 

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

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