Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #24866 - Manishearth:iterator-invalidation, r=jdm
Fix iterator invalidation in our forEach implementation. Currently we iterate over iterables in forEach based on the length they report when we start iterating, however the inner callback is able to change this. ```js let params = new URLSearchParams("foo=bar&baz=qux"); params.forEach((p) => { console.log(p); params.delete("baz"); }) ``` This causes us to panic [here](https://github.com/servo/servo/blob/f1aa5d8dbdc32f6f474b09234558652b9bead686/components/script/dom/bindings/codegen/CodegenRust.py#L7412) over an attempt to access out of bounds. Relevant spec: https://heycam.github.io/webidl/#es-forEach r? @jdm
- Loading branch information
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
tests/wpt/web-platform-tests/WebIDL/ecmascript-binding/iterator-invalidation-foreach.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>Behavior of iterators when modified within foreach</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<link rel="help" href="https://heycam.github.io/webidl/#es-forEach"> | ||
<link rel="author" title="Manish Goregaokar" href="mailto:manishsmail@gmail.com"> | ||
<script> | ||
test(function() { | ||
let params = new URLSearchParams("a=1&b=2&c=3"); | ||
let arr = []; | ||
params.forEach((p) => { | ||
arr.push(p); | ||
params.delete("b"); | ||
}) | ||
assert_array_equals(arr, ["1", "3"]); | ||
}, "forEach will not iterate over elements removed during iteration"); | ||
test(function() { | ||
let params = new URLSearchParams("a=1&b=2&c=3&d=4"); | ||
let arr = []; | ||
params.forEach((p) => { | ||
arr.push(p); | ||
if (p == "2") { | ||
params.delete("a"); | ||
} | ||
}) | ||
assert_array_equals(arr, ["1", "2", "4"]); | ||
}, "Removing elements already iterated over during forEach will cause iterator to skip an element"); | ||
test(function() { | ||
let params = new URLSearchParams("a=1&b=2&c=3&d=4"); | ||
let arr = []; | ||
params.forEach((p) => { | ||
arr.push(p); | ||
if (p == "2") { | ||
params.append("e", "5"); | ||
} | ||
}) | ||
assert_array_equals(arr, ["1", "2", "3", "4", "5"]); | ||
}, "Elements added during iteration with forEach will be reached"); | ||
</script> |