Skip to content

Commit

Permalink
libcore: fix overflow/underflow in range_step
Browse files Browse the repository at this point in the history
  • Loading branch information
gifnksm committed Apr 7, 2013
1 parent 44d4d6d commit 89676d6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
28 changes: 24 additions & 4 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ pub fn range_step(start: T, stop: T, step: T, it: &fn(T) -> bool) {
} else if step > 0 { // ascending
while i < stop {
if !it(i) { break }
// avoiding overflow. break if i + step > max_value
if i > max_value - step { break; }
i += step;
}
} else { // descending
while i > stop {
if !it(i) { break }
// avoiding underflow. break if i + step < min_value
if i < min_value - step { break; }
i += step;
}
}
Expand Down Expand Up @@ -421,10 +425,26 @@ pub fn test_ranges() {
for range_step(36,30,-2) |i| {
l.push(i);
}
assert!(l == ~[0,1,2,
13,12,11,
20,22,24,
36,34,32]);
for range_step(max_value - 2, max_value, 2) |i| {
l.push(i);
}
for range_step(max_value - 3, max_value, 2) |i| {
l.push(i);
}
for range_step(min_value + 2, min_value, -2) |i| {
l.push(i);
}
for range_step(min_value + 3, min_value, -2) |i| {
l.push(i);
}
assert_eq!(l, ~[0,1,2,
13,12,11,
20,22,24,
36,34,32,
max_value-2,
max_value-3,max_value-1,
min_value+2,
min_value+3,min_value+1]);
// None of the `fail`s should execute.
for range(10,0) |_i| {
Expand Down
31 changes: 25 additions & 6 deletions src/libcore/num/uint-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ pub fn range_step(start: T,
if step >= 0 {
while i < stop {
if !it(i) { break }
// avoiding overflow. break if i + step > max_value
if i > max_value - (step as T) { break; }
i += step as T;
}
}
else {
} else {
while i > stop {
if !it(i) { break }
// avoiding underflow. break if i + step < min_value
if i < min_value + ((-step) as T) { break; }
i -= -step as T;
}
}
Expand Down Expand Up @@ -371,11 +374,27 @@ pub fn test_ranges() {
for range_step(36,30,-2) |i| {
l.push(i);
}
for range_step(max_value - 2, max_value, 2) |i| {
l.push(i);
}
for range_step(max_value - 3, max_value, 2) |i| {
l.push(i);
}
for range_step(min_value + 2, min_value, -2) |i| {
l.push(i);
}
for range_step(min_value + 3, min_value, -2) |i| {
l.push(i);
}
assert!(l == ~[0,1,2,
13,12,11,
20,22,24,
36,34,32]);
assert_eq!(l, ~[0,1,2,
13,12,11,
20,22,24,
36,34,32,
max_value-2,
max_value-3,max_value-1,
min_value+2,
min_value+3,min_value+1]);
// None of the `fail`s should execute.
for range(0,0) |_i| {
Expand Down

5 comments on commit 89676d6

@bors
Copy link
Contributor

@bors bors commented on 89676d6 Apr 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 89676d6 Apr 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging gifnksm/rust/range_step = 89676d6 into auto

@bors
Copy link
Contributor

@bors bors commented on 89676d6 Apr 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gifnksm/rust/range_step = 89676d6 merged ok, testing candidate = 412a070

@bors
Copy link
Contributor

@bors bors commented on 89676d6 Apr 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 89676d6 Apr 9, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 412a070

Please sign in to comment.