Skip to content

Commit

Permalink
Restore original overflow checking in Fixnum step logic.
Browse files Browse the repository at this point in the history
headius committed Apr 16, 2018

Verified

This commit was signed with the committer’s verified signature.
wyattjoh Wyatt Johnson
1 parent 7464f3a commit be44d2d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions core/src/main/java/org/jruby/RubyNumeric.java
Original file line number Diff line number Diff line change
@@ -1050,20 +1050,29 @@ private static void fixnumStep(ThreadContext context, Ruby runtime, RubyFixnum f
long i = from.getLongValue();
long diff = step.getLongValue();

// We must avoid integer overflows in "i += step".
if (inf) {
for (;; i += diff) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
} else {
// We must avoid integer overflows in "i += step".
long end = ((RubyFixnum) to).getLongValue();

if (desc) {
for (; i >= end && i < Long.MAX_VALUE; i += diff) {
long tov = Long.MIN_VALUE - diff;
if (end > tov) tov = end;
for (; i >= tov; i += diff) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
if (i >= end) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
} else {
for (; i <= end && i > Long.MIN_VALUE; i += diff) {
long tov = Long.MAX_VALUE - diff;
if (end < tov) tov = end;
for (; i <= tov; i += diff) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
if (i <= end) {
block.yield(context, RubyFixnum.newFixnum(runtime, i));
}
}

0 comments on commit be44d2d

Please sign in to comment.