Skip to content

Commit 4858cfa

Browse files
Merge branch 'PHP-8.5'
* PHP-8.5: Fix GH-19926: reset internal pointer earlier while splicing array while COW violation flag is still set (#19929)
2 parents a57fe1b + b40e479 commit 4858cfa

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ PHP NEWS
1111
with a given skeleton, locale, collapse type and identity fallback.
1212
(BogdanUngureanu)
1313

14+
- Standard:
15+
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
16+
while COW violation flag is still set). (alexandre-daubois)
17+
1418
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

ext/standard/array.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3489,6 +3489,12 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
34893489
HT_SET_ITERATORS_COUNT(in_hash, 0);
34903490
in_hash->pDestructor = NULL;
34913491

3492+
/* Set internal pointer to 0 directly instead of calling zend_hash_internal_pointer_reset().
3493+
* This avoids the COW violation assertion and delays advancing to the first valid position
3494+
* until after we've switched to the new array structure (out_hash). The iterator will be
3495+
* advanced when actually accessed, at which point it will find valid indexes in the new array. */
3496+
in_hash->nInternalPointer = 0;
3497+
34923498
if (UNEXPECTED(GC_DELREF(in_hash) == 0)) {
34933499
/* Array was completely deallocated during the operation */
34943500
zend_array_destroy(in_hash);
@@ -3507,8 +3513,6 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
35073513
in_hash->nNextFreeElement = out_hash.nNextFreeElement;
35083514
in_hash->arData = out_hash.arData;
35093515
in_hash->pDestructor = out_hash.pDestructor;
3510-
3511-
zend_hash_internal_pointer_reset(in_hash);
35123516
}
35133517
/* }}} */
35143518

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-19926 (Assertion failure zend_hash_internal_pointer_reset_ex)
3+
--FILE--
4+
<?php
5+
class ThrowingDestructor {
6+
function __destruct() {
7+
throw new Exception();
8+
}
9+
}
10+
11+
$arr = [new ThrowingDestructor(), new ThrowingDestructor()];
12+
13+
try {
14+
array_splice($arr, 0, 2);
15+
} catch (Exception $e) {
16+
echo "Exception caught, no assertion failure\n";
17+
}
18+
?>
19+
--EXPECT--
20+
Exception caught, no assertion failure
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-19926 (internal pointer behavior after array_splice)
3+
--FILE--
4+
<?php
5+
$a = [1, 2, 3];
6+
unset($a[0]);
7+
next($a);
8+
9+
echo "Before array_splice: ";
10+
var_dump(current($a));
11+
12+
array_splice($a, 0, 0, [999]);
13+
14+
echo "After array_splice: ";
15+
var_dump(current($a));
16+
?>
17+
--EXPECT--
18+
Before array_splice: int(3)
19+
After array_splice: int(999)

0 commit comments

Comments
 (0)