Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Number::StepIterator overflow #10295

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions spec/std/number_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,24 @@ describe "Number" do
end
end
end

describe "whole range" do
it { (UInt8::MIN..UInt8::MAX).each.count { true }.should eq(256) }
it_iterates "UInt8 upwards", (UInt8::MIN.to_i..UInt8::MAX.to_i).map(&.to_u8), (UInt8::MIN..UInt8::MAX).step(by: 1)
it_iterates "UInt8 downwards", (UInt8::MIN.to_i..UInt8::MAX.to_i).map(&.to_u8).reverse, (UInt8::MAX..UInt8::MIN).step(by: -1)

it { (Int8::MIN..Int8::MAX).each.count { true }.should eq(256) }
it_iterates "Int8 upwards", (Int8::MIN.to_i..Int8::MAX.to_i).map(&.to_i8), (Int8::MIN..Int8::MAX).step(by: 1)
it_iterates "Int8 downwards", (Int8::MIN.to_i..Int8::MAX.to_i).map(&.to_i8).reverse, (Int8::MAX..Int8::MIN).step(by: -1)

it { (Int16::MIN..Int16::MAX).each.count { true }.should eq(65536) }
it_iterates "Int16 upwards", (Int16::MIN.to_i..Int16::MAX.to_i).map(&.to_i16), (Int16::MIN..Int16::MAX).step(by: 1)
it_iterates "Int16 downwards", (Int16::MIN.to_i..Int16::MAX.to_i).map(&.to_i16).reverse, (Int16::MAX..Int16::MIN).step(by: -1)
end

it_iterates "towards limit [max-4, max-2, max]", [Int32::MAX - 4, Int32::MAX - 2, Int32::MAX], (Int32::MAX - 4).step(to: Int32::MAX, by: 2)
it_iterates "towards limit [max-4, max-2, max)", [Int32::MAX - 4, Int32::MAX - 2], (Int32::MAX - 4).step(to: Int32::MAX, by: 2, exclusive: true)
it_iterates "towards limit [max-3, max-1, max)", [Int32::MAX - 3, Int32::MAX - 1], (Int32::MAX - 3).step(to: Int32::MAX, by: 2)
end

floor_division_returns_lhs_type {{BUILTIN_NUMBER_TYPES}}, {{BUILTIN_NUMBER_TYPES}}
Expand Down
5 changes: 2 additions & 3 deletions src/number.cr
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct Number
while true
# only proceed if difference to limit is at least as big as step size to
# avoid potential overflow errors.
sign = ((limit - current) <=> step).try(&.sign)
sign = ((limit - step) <=> current).try(&.sign)
break unless sign == direction || (sign == 0 && !exclusive)

current += step
Expand Down Expand Up @@ -246,7 +246,7 @@ struct Number
@current
elsif limit
# compare distance to current with step size
case (limit - @current <=> @step).try(&.sign)
case ((limit - @step) <=> @current).try(&.sign)
when @step.sign
# distance is more than step size, so iteration proceeds
@current += @step
Expand All @@ -262,7 +262,6 @@ struct Number
# we've either overshot the limit or the comparison failed, so we can't
# continue
@reached_end = true

stop
end
else
Expand Down