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

Nodes: Infinite loop possible when for loop uses a variable as its step value #2047

Closed
dstepanov opened this issue Jan 16, 2012 · 1 comment
Labels

Comments

@dstepanov
Copy link

When using for with a range and a step value CS is determining the iteration condition by its sign in a case when value is available, otherwise it will compare min and max values of the range definition.

I don't think it is the correct behaviour I would expect it to use the step value to determine its condition.

Example:

for i in [@min..@max] by 1
  alert i
var i, _ref, _ref2;
for (i = _ref = this.min, _ref2 = this.max; i <= _ref2; i += 1) {
  alert(i);
}

Negative step value with change the condition:

for i in [@min..@max] by -1
  alert i
var i, _ref, _ref2;
for (i = _ref = this.min, _ref2 = this.max; i >= _ref2; i += -1) {
  alert(i);
}

Unknown step value will not effect the condition of the loop in runtime:

for i in [@min..@max] by @steps
  alert i
var i, _ref, _ref2, _ref3;
for (i = _ref = this.min, _ref2 = this.max, _ref3 = this.steps; _ref <= _ref2 ? i <= _ref2 : i >= _ref2; i += _ref3) {
  alert(i);
}

In my case I can't see an additional if to be useful, when min > max and a step value is positive it will cause an infinite loop.

@tokland
Copy link

tokland commented Sep 15, 2012

I was just bitten by this (v1.3.3):

coffee> step = 1
coffee> xs = (x for x in [3..1] by step)
FATAL ERROR: JS Allocation failed - process out of memory

@GeoffreyBooth GeoffreyBooth changed the title Range loop condition shouldn't ignore step value Infinite loop possible when for loop uses a variable as its step value Apr 29, 2017
@GeoffreyBooth GeoffreyBooth changed the title Infinite loop possible when for loop uses a variable as its step value Nodes: Infinite loop possible when for loop uses a variable as its step value May 5, 2017
zdenko added a commit to zdenko/coffeescript that referenced this issue Jan 12, 2018
zdenko added a commit to zdenko/coffeescript that referenced this issue Jan 12, 2018
zdenko added a commit to zdenko/coffeescript that referenced this issue Jan 12, 2018
GeoffreyBooth pushed a commit that referenced this issue Feb 1, 2018
…ariables (#4853)

* fix #2047

* Additional check for 'step'; tests

* Fix #4105 (#4855)

* Update output

* Throw warning for unsupported runtimes, e.g. Node < 6 (#4839)

* fix #1403 (#4854)

* Update output

* [Change]: Destructuring with non-final spread should still use rest syntax (#4517) (#4825)

* destructuring optimization

* refactor

* minor improvement, fix errors

* minor refactoring

* improvements

* Update output

* Update output

* Fix #4843: bad output when assigning to @prop in destructuring assignment with defaults (#4848)

* fix #4843

* improvements

* typo

* small fix

* Fix #3441: parentheses wrapping expression throw invalid error  (#4849)

* fix #3441

* improvements

* refactor

* Fix #1726: expression in property access causes unexpected results (#4851)

* fix #1726

* Explain what's happening, rather than just linking to an issue

* Updated output

* Optimization

* Update output

* remove casting to number

* cleanup tests
@GeoffreyBooth GeoffreyBooth added fixed and removed bug labels Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants