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

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45187 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-02-27 03:10:12 +00:00
parent 8dda946059
commit c1fc20124c
3 changed files with 19 additions and 0 deletions

View file

@ -1,3 +1,9 @@
Thu Feb 27 12:10:09 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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@zzak.io> Thu Feb 27 03:55:45 2014 Zachary Scott <e@zzak.io>
* thread.c: [DOC] Typo in comment for _FORTIFY_SOURCE [Fixes GH-548] * thread.c: [DOC] Typo in comment for _FORTIFY_SOURCE [Fixes GH-548]

View file

@ -1808,10 +1808,21 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
long delta, diff, result; long delta, diff, result;
diff = FIX2LONG(step); diff = FIX2LONG(step);
if (!diff) rb_num_zerodiv();
delta = FIX2LONG(to) - FIX2LONG(from); delta = FIX2LONG(to) - FIX2LONG(from);
if (excl) { if (excl) {
delta += (diff > 0 ? -1 : +1); 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; result = delta / diff;
return LONG2FIX(result >= 0 ? result + 1 : 0); return LONG2FIX(result >= 0 ? result + 1 : 0);
} }

View file

@ -279,6 +279,8 @@ class TestNumeric < Test::Unit::TestCase
assert_step [1], [1, 10, 2**32] assert_step [1], [1, 10, 2**32]
assert_step [1], [1, to: 10, by: 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 [3, 3, 3, 3], [3, by: 0], inf: true
assert_step [10], [10, 1, -(2**32)] assert_step [10], [10, 1, -(2**32)]