Skip to content

Commit b64aa3a

Browse files
jacobqkategengler
authored andcommitted
test(NativeArray): Check for cyclic Array.prototype (ref #17190)
(cherry picked from commit 3029e9d)
1 parent e8ce55c commit b64aa3a

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { NativeArray } from '../../lib/mixins/array';
2+
import { AbstractTestCase, moduleFor } from 'internal-test-helpers';
3+
4+
class ArrayPrototypeExtensionSelfReferenceTests extends AbstractTestCase {
5+
'@test should not create non-Symbol, enumerable properties that refer to itself'() {
6+
// Don't want to pollute Array.prototype so we make our own to extend
7+
class ThrowAwayArray extends Array {}
8+
9+
// Extend our throw-away prototype (like EXTEND_PROTOTYPES.Array would)
10+
NativeArray.apply(ThrowAwayArray.prototype);
11+
12+
// Create an instance to test
13+
let obj = new ThrowAwayArray();
14+
15+
// Make sure we have an array-like thing & avoid the zero assertion problem is there are no enumerable properties
16+
this.assert.strictEqual(obj.length, 0);
17+
18+
// Make sure that no enumerable properties refer back to the object (creating a cyclic structure)
19+
for (let p in obj) {
20+
this.assert.notStrictEqual(
21+
obj[p],
22+
obj,
23+
`Property "${p}" is an enumerable part of the prototype
24+
so must not refer back to the original array.
25+
Otherwise code that explores all properties,
26+
such as jQuery.extend and other "deep cloning" functions,
27+
will get stuck in an infinite loop.
28+
`.replace(/\s+/g, ' ')
29+
);
30+
}
31+
}
32+
}
33+
34+
moduleFor(`NativeArray: apply`, ArrayPrototypeExtensionSelfReferenceTests);

0 commit comments

Comments
 (0)