Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions ext/spl/spl_dllist.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,10 @@ PHP_METHOD(SplDoublyLinkedList, offsetUnset)
element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);

if (element != NULL) {
/* connect the neightbors */
/* disconnect the neighbours */
if (element->prev) {
element->prev->next = element->next;
}

if (element->next) {
element->next->prev = element->prev;
}
Expand All @@ -782,6 +781,10 @@ PHP_METHOD(SplDoublyLinkedList, offsetUnset)
llist->tail = element->prev;
}

/* Keep consistency if element is kept alive. */
element->prev = NULL;
element->next = NULL;

/* finally, delete the element */
llist->count--;

Expand Down
26 changes: 26 additions & 0 deletions ext/spl/tests/gh20856.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-20856 (heap-use-after-free in SplDoublyLinkedList iterator when modifying during iteration)
--CREDITS--
vi3tL0u1s
iluuu1994
--FILE--
<?php
$m = new SplStack;
$m[] = new stdClass;
$m[] = new stdClass;

foreach ($m as $l) {
unset($m[0]);
unset($m[0]);
}

var_dump($m);
?>
--EXPECTF--
object(SplStack)#%d (%d) {
["flags":"SplDoublyLinkedList":private]=>
int(6)
["dllist":"SplDoublyLinkedList":private]=>
array(0) {
}
}
Loading