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

Commit db9f257

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 4f45bf1 commit db9f257

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
@@ -498,7 +498,7 @@ function $RootScopeProvider(){
498498

499499
function $watchCollectionWatch() {
500500
newValue = objGetter(self);
501-
var newLength, key;
501+
var newLength, key, bothNaN;
502502

503503
if (!isObject(newValue)) { // if primitive
504504
if (oldValue !== newValue) {
@@ -522,7 +522,7 @@ function $RootScopeProvider(){
522522
}
523523
// copy the items to oldValue and look for changes.
524524
for (var i = 0; i < newLength; i++) {
525-
var bothNaN = (oldValue[i] !== oldValue[i]) &&
525+
bothNaN = (oldValue[i] !== oldValue[i]) &&
526526
(newValue[i] !== newValue[i]);
527527
if (!bothNaN && (oldValue[i] !== newValue[i])) {
528528
changeDetected++;
@@ -542,7 +542,9 @@ function $RootScopeProvider(){
542542
if (newValue.hasOwnProperty(key)) {
543543
newLength++;
544544
if (oldValue.hasOwnProperty(key)) {
545-
if (oldValue[key] !== newValue[key]) {
545+
bothNaN = (oldValue[key] !== oldValue[key]) &&
546+
(newValue[key] !== newValue[key]);
547+
if (!bothNaN && (oldValue[key] !== newValue[key])) {
546548
changeDetected++;
547549
oldValue[key] = newValue[key];
548550
}

test/ng/rootScopeSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,14 @@ describe('Scope', function() {
746746
$rootScope.$digest();
747747
expect(log.empty()).toEqual([{newVal: {b: {}, c: 'B'}, oldVal: {a: [], b: {}, c: 'B'}}]);
748748
});
749+
750+
it('should not infinitely digest when current value is NaN', function() {
751+
$rootScope.obj = {a: NaN};
752+
expect(function() {
753+
$rootScope.$digest();
754+
}).not.toThrow();
755+
});
756+
749757
});
750758
});
751759

0 commit comments

Comments
 (0)