Skip to content

Commit b2ec6c2

Browse files
committed
Fix exception handling in array_multisort()
Closes GH-11302
1 parent f5c54fd commit b2ec6c2

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ PHP NEWS
44

55
- Standard:
66
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
7+
. Fix exception handling in array_multisort(). (ilutov)
78

89
08 Jun 2023, PHP 8.1.20
910

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Exception handling in array_multisort()
3+
--FILE--
4+
<?php
5+
$array = [1 => new DateTime(), 0 => new DateTime()];
6+
array_multisort($array, SORT_STRING);
7+
?>
8+
--EXPECTF--
9+
Fatal error: Uncaught Error: Object of class DateTime could not be converted to string in %s:%d
10+
Stack trace:
11+
#0 %s(%d): array_multisort(Array, 2)
12+
#1 {main}
13+
thrown in %s on line %d

ext/standard/array.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,6 +5598,9 @@ PHP_FUNCTION(array_multisort)
55985598

55995599
/* Do the actual sort magic - bada-bim, bada-boom. */
56005600
zend_sort(indirect, array_size, sizeof(Bucket *), php_multisort_compare, (swap_func_t)array_bucket_p_sawp);
5601+
if (EG(exception)) {
5602+
goto clean_up;
5603+
}
56015604

56025605
/* Restructure the arrays based on sorted indirect - this is mostly taken from zend_hash_sort() function. */
56035606
for (i = 0; i < num_arrays; i++) {
@@ -5623,15 +5626,15 @@ PHP_FUNCTION(array_multisort)
56235626
zend_hash_rehash(hash);
56245627
}
56255628
}
5629+
RETVAL_TRUE;
56265630

5627-
/* Clean up. */
5631+
clean_up:
56285632
for (i = 0; i < array_size; i++) {
56295633
efree(indirect[i]);
56305634
}
56315635
efree(indirect);
56325636
efree(func);
56335637
efree(arrays);
5634-
RETURN_TRUE;
56355638
}
56365639
/* }}} */
56375640

0 commit comments

Comments
 (0)