Skip to content

Commit ad66273

Browse files
committed
fix buffer mutation exposure in Iterator.prototype.windows polyfill
1 parent ad7c306 commit ad66273

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- Fixed some cases of iterators closing in `Iterator.{ zip, zipKeyed }` polyfills
2121
- Fixed validation of iterators `.next()` results an objects in `Iterator.{ zip, zipKeyed }` polyfills
2222
- Fixed a lack of early error in `Iterator.concat` polyfill on primitive as an iterator
23+
- Fixed buffer mutation exposure in `Iterator.prototype.windows` polyfill
2324
- Fixed iterator closing in `Set.prototype.{ isDisjointFrom, isSupersetOf }` polyfill
2425
- Fixed (updated following the final spec) one more case `Set.prototype.difference` polyfill with updating `this`
2526
- Fixed `DataView.prototype.setFloat16` polyfill in (0, 1) range

packages/core-js/internals/iterator-window.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ var IteratorProxy = createIteratorProxy(function () {
2323
while (true) {
2424
result = anObject(call(next, iterator));
2525
done = this.done = !!result.done;
26-
if (allowPartial && done && buffer.length && buffer.length < windowSize) return createIterResultObject(buffer, false);
26+
if (allowPartial && done && buffer.length && buffer.length < windowSize) return createIterResultObject(slice(buffer, 0), false);
2727
if (done) return createIterResultObject(undefined, true);
2828

2929
if (buffer.length === windowSize) this.buffer = buffer = slice(buffer, 1);
3030
push(buffer, result.value);
31-
if (buffer.length === windowSize) return createIterResultObject(buffer, false);
31+
if (buffer.length === windowSize) return createIterResultObject(slice(buffer, 0), false);
3232
}
3333
}, false, true);
3434

tests/unit-global/esnext.iterator.windows.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ QUnit.test('Iterator#windows', assert => {
5151

5252
assert.throws(() => windows.call(createIterator([1]), 2, null), TypeError, 'incorrect `undersized` argument #1');
5353
assert.throws(() => windows.call(createIterator([1]), 2, 'allowpartial'), TypeError, 'incorrect `undersized` argument #2');
54+
55+
// windows should return independent copies (not the same buffer)
56+
{
57+
const iter = windows.call(createIterator([1, 2, 3, 4, 5]), 3);
58+
const w1 = iter.next().value;
59+
assert.deepEqual(w1, [1, 2, 3], 'window 1');
60+
w1[1] = 99;
61+
const w2 = iter.next().value;
62+
assert.deepEqual(w2, [2, 3, 4], 'window 2 not affected by mutation of window 1');
63+
}
5464
});

tests/unit-pure/esnext.iterator.windows.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ QUnit.test('Iterator#windows', assert => {
5050

5151
assert.throws(() => windows.call(createIterator([1]), 2, null), TypeError, 'incorrect `undersized` argument #1');
5252
assert.throws(() => windows.call(createIterator([1]), 2, 'allowpartial'), TypeError, 'incorrect `undersized` argument #2');
53+
54+
// windows should return independent copies (not the same buffer)
55+
{
56+
const iter = windows.call(createIterator([1, 2, 3, 4, 5]), 3);
57+
const w1 = iter.next().value;
58+
assert.deepEqual(w1, [1, 2, 3], 'window 1');
59+
w1[1] = 99;
60+
const w2 = iter.next().value;
61+
assert.deepEqual(w2, [2, 3, 4], 'window 2 not affected by mutation of window 1');
62+
}
5363
});

0 commit comments

Comments
 (0)