Skip to content

Commit 8ebae84

Browse files
committed
Fixed bug #77395 (segfault about array_multisort)
1 parent b0cfa28 commit 8ebae84

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ PHP NEWS
99
. Fixed bug #76839 (socket_recvfrom may return an invalid 'from' address
1010
on MacOS). (Michael Meyer)
1111

12+
- Standard:
13+
. Fixed bug #77395 (segfault about array_multisort). (Laruence)
14+
1215
03 Jan 2019, PHP 7.2.14
1316

1417
- Core:

ext/standard/array.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5555,7 +5555,7 @@ PHPAPI int php_multisort_compare(const void *a, const void *b) /* {{{ */
55555555
/* }}} */
55565556

55575557
#define MULTISORT_ABORT \
5558-
efree(ARRAYG(multisort_func)); \
5558+
efree(func); \
55595559
efree(arrays); \
55605560
RETURN_FALSE;
55615561

@@ -5587,6 +5587,7 @@ PHP_FUNCTION(array_multisort)
55875587
int sort_order = PHP_SORT_ASC;
55885588
int sort_type = PHP_SORT_REGULAR;
55895589
int i, k, n;
5590+
compare_func_t *func;
55905591

55915592
ZEND_PARSE_PARAMETERS_START(1, -1)
55925593
Z_PARAM_VARIADIC('+', args, argc)
@@ -5597,7 +5598,7 @@ PHP_FUNCTION(array_multisort)
55975598
for (i = 0; i < MULTISORT_LAST; i++) {
55985599
parse_state[i] = 0;
55995600
}
5600-
ARRAYG(multisort_func) = (compare_func_t*)ecalloc(argc, sizeof(compare_func_t));
5601+
func = ARRAYG(multisort_func) = (compare_func_t*)ecalloc(argc, sizeof(compare_func_t));
56015602

56025603
/* Here we go through the input arguments and parse them. Each one can
56035604
* be either an array or a sort flag which follows an array. If not
@@ -5681,7 +5682,7 @@ PHP_FUNCTION(array_multisort)
56815682

56825683
/* If all arrays are empty we don't need to do anything. */
56835684
if (array_size < 1) {
5684-
efree(ARRAYG(multisort_func));
5685+
efree(func);
56855686
efree(arrays);
56865687
RETURN_TRUE;
56875688
}
@@ -5740,7 +5741,7 @@ PHP_FUNCTION(array_multisort)
57405741
efree(indirect[i]);
57415742
}
57425743
efree(indirect);
5743-
efree(ARRAYG(multisort_func));
5744+
efree(func);
57445745
efree(arrays);
57455746
RETURN_TRUE;
57465747
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #77395 (segfault about array_multisort)
3+
--FILE--
4+
<?php
5+
function error_handle($level, $message, $file = '', $line = 0){
6+
$a = [1,2,3];
7+
$b = [3,2,1];
8+
echo $message;
9+
array_multisort($a, SORT_ASC, $b); // if comment this line, no segfault happen
10+
}
11+
set_error_handler('error_handle');
12+
$data = [['aa'=> 'bb',], ['aa'=> 'bb',],];
13+
array_multisort(array_column($data, 'bb'),SORT_DESC, $data); // PHP Warning error
14+
?>
15+
--EXPECT--
16+
array_multisort(): Array sizes are inconsistent

0 commit comments

Comments
 (0)