Skip to content

Commit 14fddd1

Browse files
committed
Fix arsort() crash on recursion
Fixes oss-fuzz #46315
1 parent e4c7ffc commit 14fddd1

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
@@ -2551,6 +2551,16 @@ ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, b
25512551
ht->nNumUsed = i;
25522552
}
25532553

2554+
if (!(HT_FLAGS(ht) & HASH_FLAG_PACKED)) {
2555+
/* We broke the hash colisions chains overriding Z_NEXT() by Z_EXTRA().
2556+
* Reset the hash headers table as well to avoid possilbe inconsistent
2557+
* access on recursive data structures.
2558+
*
2559+
* See Zend/tests/bug63882_2.phpt
2560+
*/
2561+
HT_HASH_RESET(ht);
2562+
}
2563+
25542564
sort((void *)ht->arData, ht->nNumUsed, sizeof(Bucket), (compare_func_t) compar,
25552565
(swap_func_t)(renumber? zend_hash_bucket_renum_swap :
25562566
((HT_FLAGS(ht) & HASH_FLAG_PACKED) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));

0 commit comments

Comments
 (0)