Circular references in arrays blows up
remy opened this issue · 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
Reason
- array is dirtied
- loop through array
- goes into first level
- 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
}