Skip to content

Commit d8d754f

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix phpGH-20856: heap-use-after-free in SplDoublyLinkedList iterator when modifying during iteration
2 parents c434e04 + a82a93d commit d8d754f

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

ext/spl/spl_dllist.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,10 @@ PHP_METHOD(SplDoublyLinkedList, offsetUnset)
728728
element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
729729

730730
if (element != NULL) {
731-
/* connect the neighbors */
731+
/* disconnect the neighbours */
732732
if (element->prev) {
733733
element->prev->next = element->next;
734734
}
735-
736735
if (element->next) {
737736
element->next->prev = element->prev;
738737
}
@@ -746,6 +745,10 @@ PHP_METHOD(SplDoublyLinkedList, offsetUnset)
746745
llist->tail = element->prev;
747746
}
748747

748+
/* Keep consistency if element is kept alive. */
749+
element->prev = NULL;
750+
element->next = NULL;
751+
749752
/* finally, delete the element */
750753
llist->count--;
751754

ext/spl/tests/gh20856.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-20856 (heap-use-after-free in SplDoublyLinkedList iterator when modifying during iteration)
3+
--CREDITS--
4+
vi3tL0u1s
5+
iluuu1994
6+
--FILE--
7+
<?php
8+
$m = new SplStack;
9+
$m[] = new stdClass;
10+
$m[] = new stdClass;
11+
12+
foreach ($m as $l) {
13+
unset($m[0]);
14+
unset($m[0]);
15+
}
16+
17+
var_dump($m);
18+
?>
19+
--EXPECTF--
20+
object(SplStack)#%d (%d) {
21+
["flags":"SplDoublyLinkedList":private]=>
22+
int(6)
23+
["dllist":"SplDoublyLinkedList":private]=>
24+
array(0) {
25+
}
26+
}

0 commit comments

Comments
 (0)