Fix exception handling in array_multisort()

Closes GH-11302
This commit is contained in:
Ilija Tovilo 2023-05-23 09:54:54 +02:00
parent f5c54fd88b
commit b2ec6c24f8
No known key found for this signature in database
GPG key ID: A4F5D403F118200A
3 changed files with 19 additions and 2 deletions

1
NEWS
View file

@ -4,6 +4,7 @@ PHP NEWS
- Standard: - Standard:
. Fix access on NULL pointer in array_merge_recursive(). (ilutov) . Fix access on NULL pointer in array_merge_recursive(). (ilutov)
. Fix exception handling in array_multisort(). (ilutov)
08 Jun 2023, PHP 8.1.20 08 Jun 2023, PHP 8.1.20

View file

@ -0,0 +1,13 @@
--TEST--
Exception handling in array_multisort()
--FILE--
<?php
$array = [1 => new DateTime(), 0 => new DateTime()];
array_multisort($array, SORT_STRING);
?>
--EXPECTF--
Fatal error: Uncaught Error: Object of class DateTime could not be converted to string in %s:%d
Stack trace:
#0 %s(%d): array_multisort(Array, 2)
#1 {main}
thrown in %s on line %d

View file

@ -5598,6 +5598,9 @@ PHP_FUNCTION(array_multisort)
/* Do the actual sort magic - bada-bim, bada-boom. */ /* Do the actual sort magic - bada-bim, bada-boom. */
zend_sort(indirect, array_size, sizeof(Bucket *), php_multisort_compare, (swap_func_t)array_bucket_p_sawp); zend_sort(indirect, array_size, sizeof(Bucket *), php_multisort_compare, (swap_func_t)array_bucket_p_sawp);
if (EG(exception)) {
goto clean_up;
}
/* Restructure the arrays based on sorted indirect - this is mostly taken from zend_hash_sort() function. */ /* Restructure the arrays based on sorted indirect - this is mostly taken from zend_hash_sort() function. */
for (i = 0; i < num_arrays; i++) { for (i = 0; i < num_arrays; i++) {
@ -5623,15 +5626,15 @@ PHP_FUNCTION(array_multisort)
zend_hash_rehash(hash); zend_hash_rehash(hash);
} }
} }
RETVAL_TRUE;
/* Clean up. */ clean_up:
for (i = 0; i < array_size; i++) { for (i = 0; i < array_size; i++) {
efree(indirect[i]); efree(indirect[i]);
} }
efree(indirect); efree(indirect);
efree(func); efree(func);
efree(arrays); efree(arrays);
RETURN_TRUE;
} }
/* }}} */ /* }}} */