Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix($rootScope): $watchCollection should handle NaN in objects
Browse files Browse the repository at this point in the history
This fixes a potential infinite digest in $watchCollection when one of the values is NaN. This was previously fixed for arrays, but needs to be handled for objects as well.

Closes #7930
  • Loading branch information
shahata authored and rodyhaddad committed Jul 15, 2014
1 parent fe01a85 commit bf13d26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ function $RootScopeProvider(){

function $watchCollectionWatch() {
newValue = objGetter(self);
var newLength, key;
var newLength, key, bothNaN;

if (!isObject(newValue)) { // if primitive
if (oldValue !== newValue) {
Expand All @@ -467,7 +467,7 @@ function $RootScopeProvider(){
}
// copy the items to oldValue and look for changes.
for (var i = 0; i < newLength; i++) {
var bothNaN = (oldValue[i] !== oldValue[i]) &&
bothNaN = (oldValue[i] !== oldValue[i]) &&
(newValue[i] !== newValue[i]);
if (!bothNaN && (oldValue[i] !== newValue[i])) {
changeDetected++;
Expand All @@ -487,7 +487,9 @@ function $RootScopeProvider(){
if (newValue.hasOwnProperty(key)) {
newLength++;
if (oldValue.hasOwnProperty(key)) {
if (oldValue[key] !== newValue[key]) {
bothNaN = (oldValue[key] !== oldValue[key]) &&
(newValue[key] !== newValue[key]);
if (!bothNaN && (oldValue[key] !== newValue[key])) {
changeDetected++;
oldValue[key] = newValue[key];
}
Expand Down
8 changes: 8 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,14 @@ describe('Scope', function() {
$rootScope.$digest();
expect(log.empty()).toEqual([{newVal: {b: {}, c: 'B'}, oldVal: {a: [], b: {}, c: 'B'}}]);
});

it('should not infinitely digest when current value is NaN', function() {
$rootScope.obj = {a: NaN};
expect(function() {
$rootScope.$digest();
}).not.toThrow();
});

});
});

Expand Down

0 comments on commit bf13d26

Please sign in to comment.