Skip to content

Commit

Permalink
Fix exception handling in array_multisort()
Browse files Browse the repository at this point in the history
Closes GH-11302
  • Loading branch information
iluuu1994 committed May 24, 2023
1 parent f5c54fd commit b2ec6c2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS
Expand Up @@ -4,6 +4,7 @@ PHP NEWS

- Standard:
. Fix access on NULL pointer in array_merge_recursive(). (ilutov)
. Fix exception handling in array_multisort(). (ilutov)

08 Jun 2023, PHP 8.1.20

Expand Down
13 changes: 13 additions & 0 deletions Zend/tests/array_multisort_exception.phpt
@@ -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
7 changes: 5 additions & 2 deletions ext/standard/array.c
Expand Up @@ -5598,6 +5598,9 @@ PHP_FUNCTION(array_multisort)

/* 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);
if (EG(exception)) {
goto clean_up;
}

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

/* Clean up. */
clean_up:
for (i = 0; i < array_size; i++) {
efree(indirect[i]);
}
efree(indirect);
efree(func);
efree(arrays);
RETURN_TRUE;
}
/* }}} */

Expand Down

0 comments on commit b2ec6c2

Please sign in to comment.