Skip to content

Commit 2ab05da

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix arsort() crash on recursion
2 parents 2874e5f + 14fddd1 commit 2ab05da

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

Zend/tests/bug63882_2.phpt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #63882_2 (arsort crash on recursion)
3+
--FILE--
4+
<?php
5+
$token = array();
6+
$conditions = array();
7+
for ($i = 0; $i <= 2; $i++) {
8+
$tokens = $conditions;
9+
$a[0] =& $a;
10+
$a = unserialize(serialize($GLOBALS));
11+
$a[0] =& $a;
12+
$a = unserialize(serialize($GLOBALS));
13+
$a[0] =& $a;
14+
foreach($a as $v) {
15+
if ($v == 1) {
16+
arsort($a);
17+
}
18+
}
19+
}
20+
?>
21+
DONE
22+
--EXPECT--
23+
DONE

Zend/zend_hash.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,16 @@ ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, b
28632863
ht->nNumUsed = i;
28642864
}
28652865

2866+
if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED)) {
2867+
/* We broke the hash colisions chains overriding Z_NEXT() by Z_EXTRA().
2868+
* Reset the hash headers table as well to avoid possilbe inconsistent
2869+
* access on recursive data structures.
2870+
*
2871+
* See Zend/tests/bug63882_2.phpt
2872+
*/
2873+
HT_HASH_RESET(ht);
2874+
}
2875+
28662876
sort((void *)ht->arData, ht->nNumUsed, sizeof(Bucket), (compare_func_t) compar,
28672877
(swap_func_t)(renumber? zend_hash_bucket_renum_swap :
28682878
(HT_IS_PACKED(ht) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));

0 commit comments

Comments
 (0)