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 #2047: Infinite loop possible when for loop with range uses variables #4853

Merged
merged 16 commits into from
Feb 1, 2018
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
12 changes: 10 additions & 2 deletions lib/coffeescript/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/coffeescript/rewriter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1336,14 +1336,27 @@ exports.Range = class Range extends Base
[lt, gt] = ["#{idx} <#{@equals}", "#{idx} >#{@equals}"]

# Generate the condition.
condPart = if @stepNum?
if @stepNum > 0 then "#{lt} #{@toVar}" else "#{gt} #{@toVar}"
else if known
[from, to] = [@fromNum, @toNum]
if from <= to then "#{lt} #{to}" else "#{gt} #{to}"
else
cond = if @stepVar then "#{@stepVar} > 0" else "#{@fromVar} <= #{@toVar}"
"#{cond} ? #{lt} #{@toVar} : #{gt} #{@toVar}"
[from, to] = [@fromNum, @toNum]
# Always check if the `step` isn't zero to avoid the infinite loop.
stepCond = if @stepNum then "#{@stepNum} !== 0" else "#{@stepVar} !== 0"
condPart =
if known
unless @step?
if from <= to then "#{lt} #{to}" else "#{gt} #{to}"
else
# from < to
lowerBound = "#{from} <= #{idx} && #{lt} #{to}"
# from > to
upperBound = "#{from} >= #{idx} && #{gt} #{to}"
if from <= to then "#{stepCond} && #{lowerBound}" else "#{stepCond} && #{upperBound}"
else
# from < to
lowerBound = "#{@fromVar} <= #{idx} && #{lt} #{@toVar}"
# from > to
upperBound = "#{@fromVar} >= #{idx} && #{gt} #{@toVar}"
"#{stepCond} && (#{@fromVar} <= #{@toVar} ? #{lowerBound} : #{upperBound})"

cond = if @stepVar then "#{@stepVar} > 0" else "#{@fromVar} <= #{@toVar}"

# Generate the step.
stepPart = if @stepVar
Expand Down
79 changes: 79 additions & 0 deletions test/ranges.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,82 @@ test "#1012 slices with arguments object", ->

test "#1409: creating large ranges outside of a function body", ->
CoffeeScript.eval '[0..100]'

test "#2047: Infinite loop possible when `for` loop with `range` uses variables", ->
up = 1
down = -1
a = 1
b = 5

testRange = (arg) ->
[from, to, step, expectedResult] = arg
r = (x for x in [from..to] by step)
arrayEq r, expectedResult

testData = [
[1, 5, 1, [1..5]]
[1, 5, -1, [1]]
[1, 5, up, [1..5]]
[1, 5, down, [1]]

[a, 5, 1, [1..5]]
[a, 5, -1, [1]]
[a, 5, up, [1..5]]
[a, 5, down, [1]]

[1, b, 1, [1..5]]
[1, b, -1, [1]]
[1, b, up, [1..5]]
[1, b, down, [1]]

[a, b, 1, [1..5]]
[a, b, -1, [1]]
[a, b, up, [1..5]]
[a, b, down, [1]]

[5, 1, 1, [5]]
[5, 1, -1, [5..1]]
[5, 1, up, [5]]
[5, 1, down, [5..1]]

[5, a, 1, [5]]
[5, a, -1, [5..1]]
[5, a, up, [5]]
[5, a, down, [5..1]]

[b, 1, 1, [5]]
[b, 1, -1, [5..1]]
[b, 1, up, [5]]
[b, 1, down, [5..1]]

[b, a, 1, [5]]
[b, a, -1, [5..1]]
[b, a, up, [5]]
[b, a, down, [5..1]]
]

testRange d for d in testData

test "#2047: from, to and step as variables", ->
up = 1
down = -1
a = 1
b = 5

r = (x for x in [a..b] by up)
arrayEq r, [1..5]

r = (x for x in [a..b] by down)
arrayEq r, [1]

r = (x for x in [b..a] by up)
arrayEq r, [5]

r = (x for x in [b..a] by down)
arrayEq r, [5..1]

a = 1
b = -1
step = 0
r = (x for x in [b..a] by step)
arrayEq r, []