summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-27 03:10:12 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-27 03:10:12 +0000
commitc1fc20124c269eab6a863e48c6647884b883de88 (patch)
tree2af8bddc38c96f414ac31d7985294eae1189f050 /numeric.c
parent8dda9460590abbde03d813ee684dfdd147f3d31d (diff)
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
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/numeric.c b/numeric.c
index 838ec43c29..99a1c7d60e 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1808,10 +1808,21 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
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);
}