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

Commit bf13d26

Browse files
shahatarodyhaddad
authored andcommitted
fix($rootScope): $watchCollection should handle NaN in objects
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
1 parent fe01a85 commit bf13d26

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/ng/rootScope.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ function $RootScopeProvider(){
443443

444444
function $watchCollectionWatch() {
445445
newValue = objGetter(self);
446-
var newLength, key;
446+
var newLength, key, bothNaN;
447447

448448
if (!isObject(newValue)) { // if primitive
449449
if (oldValue !== newValue) {
@@ -467,7 +467,7 @@ function $RootScopeProvider(){
467467
}
468468
// copy the items to oldValue and look for changes.
469469
for (var i = 0; i < newLength; i++) {
470-
var bothNaN = (oldValue[i] !== oldValue[i]) &&
470+
bothNaN = (oldValue[i] !== oldValue[i]) &&
471471
(newValue[i] !== newValue[i]);
472472
if (!bothNaN && (oldValue[i] !== newValue[i])) {
473473
changeDetected++;
@@ -487,7 +487,9 @@ function $RootScopeProvider(){
487487
if (newValue.hasOwnProperty(key)) {
488488
newLength++;
489489
if (oldValue.hasOwnProperty(key)) {
490-
if (oldValue[key] !== newValue[key]) {
490+
bothNaN = (oldValue[key] !== oldValue[key]) &&
491+
(newValue[key] !== newValue[key]);
492+
if (!bothNaN && (oldValue[key] !== newValue[key])) {
491493
changeDetected++;
492494
oldValue[key] = newValue[key];
493495
}

test/ng/rootScopeSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,14 @@ describe('Scope', function() {
698698
$rootScope.$digest();
699699
expect(log.empty()).toEqual([{newVal: {b: {}, c: 'B'}, oldVal: {a: [], b: {}, c: 'B'}}]);
700700
});
701+
702+
it('should not infinitely digest when current value is NaN', function() {
703+
$rootScope.obj = {a: NaN};
704+
expect(function() {
705+
$rootScope.$digest();
706+
}).not.toThrow();
707+
});
708+
701709
});
702710
});
703711

0 commit comments

Comments
 (0)