Skip to content

Commit 68720d0

Browse files
Fix GH-19926: reset internal pointer earlier while splicing array while COW violation flag is still set
1 parent 266cb7d commit 68720d0

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHP NEWS
3636
. Fixed bug GH-19701 (Serialize/deserialize loses some data). (nielsdos)
3737
. Fixed bug GH-19801 (leaks in var_dump() and debug_zval_dump()).
3838
(alexandre-daubois)
39+
. Fixed bug GH-19926 (reset internal pointer earlier while splicing array
40+
while COW violation flag is still set). (alexandre-daubois)
3941

4042
- Zip:
4143
. Fixed bug GH-19688 (Remove pattern overflow in zip addGlob()). (nielsdos)

ext/standard/array.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,9 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
33763376
HT_SET_ITERATORS_COUNT(in_hash, 0);
33773377
in_hash->pDestructor = NULL;
33783378

3379+
/* Reset internal pointer while COW violation flag is still set */
3380+
zend_hash_internal_pointer_reset(in_hash);
3381+
33793382
if (UNEXPECTED(GC_DELREF(in_hash) == 0)) {
33803383
/* Array was completely deallocated during the operation */
33813384
zend_array_destroy(in_hash);
@@ -3394,8 +3397,6 @@ static void php_splice(HashTable *in_hash, zend_long offset, zend_long length, H
33943397
in_hash->nNextFreeElement = out_hash.nNextFreeElement;
33953398
in_hash->arData = out_hash.arData;
33963399
in_hash->pDestructor = out_hash.pDestructor;
3397-
3398-
zend_hash_internal_pointer_reset(in_hash);
33993400
}
34003401
/* }}} */
34013402

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(3)

0 commit comments

Comments
 (0)