bind.js icon indicating copy to clipboard operation
bind.js copied to clipboard

Circular references in arrays blows up

Open remy opened this issue 8 years ago • 1 comments

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

remy avatar Sep 03 '15 12:09 remy

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 
}

ghost avatar Jan 09 '16 00:01 ghost