-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types: fix potential use after free on adding keys during iteration
When keys are added to the object currently being iterated by a for loop, the insert operation might cause a hashtable resize with a subsequent memory reallocation and a different table base pointer, clobbering the entry pointers held by iterators pointing to containing object of the resized table. In order to address issue while keeping the iteration overhead low, extend the object key insert logic to check whether the insertion will trigger a reallocation and backup and restore the iterator pointers when needed. This slightly increases the size of the iterator states but the overhead for this should be neglectible as there'll only be a low amount of concurrently active iterations at any time. Fixes: #230 Signed-off-by: Jo-Philipp Wich <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
8 deletions.
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
40 changes: 40 additions & 0 deletions
40
tests/custom/99_bugs/48_use_after_free_on_iteration_insert
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 @@ | ||
Ensure that adding keys to an object currently being iterated will not | ||
clobber active iterators pointing into that object due to a reallocation | ||
of the underlying hash table array. | ||
|
||
-- Testcase -- | ||
{% | ||
let obj = { '0': 0, '1': 1 }; | ||
let i = 2; | ||
|
||
for (let k, v in obj) { | ||
while (i < 16) { | ||
obj[i] = i; | ||
i++; | ||
} | ||
} | ||
|
||
printf("%.J\n", obj); | ||
%} | ||
-- End -- | ||
|
||
-- Expect stdout -- | ||
{ | ||
"0": 0, | ||
"1": 1, | ||
"2": 2, | ||
"3": 3, | ||
"4": 4, | ||
"5": 5, | ||
"6": 6, | ||
"7": 7, | ||
"8": 8, | ||
"9": 9, | ||
"10": 10, | ||
"11": 11, | ||
"12": 12, | ||
"13": 13, | ||
"14": 14, | ||
"15": 15 | ||
} | ||
-- End -- |
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