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

Circular references in arrays blows up #10

Open
remy opened this issue Sep 3, 2015 · 1 comment
Open

Circular references in arrays blows up #10

remy opened this issue Sep 3, 2015 · 1 comment

Comments

@remy
Copy link
Owner

remy commented Sep 3, 2015

Been using bind in a private node project, and this causes a pretty serious bail:

data = new Bind({
  foo: [],
}, {
  foo: function () {},
});

data.foo.push(data.foo); // RangeError: Maximum call stack size exceeded
@ghost
Copy link

ghost commented Jan 9, 2016

Reason

  1. array is dirtied
  2. loop through array
  3. goes into first level
  4. repeat back to array and endlessly loop through this

if you go into your command line, I'm using chrome, and do something like:

arr = [];
arr[0] = arr;

You can endlessly open up each consecutive arr's first index.

arr[0] = arr
arr[0][0] = arr
arr[0][0][0] = arr
... and so forth .

FIX

When recursively looping through an object's keys/array, make sure the value associated with the key does not refer to the parent. If it is the parent, don't proceed with the recursion; just move on.

Also, you don't want extreme cases where something like this happens:

var par = { child:{ child2:[ 1, 2, "object", actualObject, par] } }
This case there will be an endless loop emerging from child2 because it refers to a higher path.

general code fix:

/// outer scope
var objectsRegistered = []; // let objects be ran through ONLY once. 


// lower scope 
if( typeof value === typeof object ){
    if ( objectsRegistered.indexOf( value ) > -1 ){
        return;
    }
    objectsRegistered.push( value );
    // do further processing here 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant