Skip to content

Commit eb7ec15

Browse files
committed
Use single allocation for indirect values in array_multisort
Closes GH-11309
1 parent 5ff244d commit eb7ec15

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: ext/standard/array.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -5782,8 +5782,10 @@ PHP_FUNCTION(array_multisort)
57825782
* of the input arrays + 1. The last column is UNDEF to indicate the end
57835783
* of the row. It also stores the original position for stable sorting. */
57845784
indirect = (Bucket **)safe_emalloc(array_size, sizeof(Bucket *), 0);
5785+
/* Move num_arrays multiplication to size because it's essentially impossible to overflow. */
5786+
Bucket *indirects = (Bucket *)safe_emalloc(array_size, sizeof(Bucket) * (num_arrays + 1), 0);
57855787
for (i = 0; i < array_size; i++) {
5786-
indirect[i] = (Bucket *)safe_emalloc((num_arrays + 1), sizeof(Bucket), 0);
5788+
indirect[i] = indirects + (i * (num_arrays + 1));
57875789
}
57885790
for (i = 0; i < num_arrays; i++) {
57895791
k = 0;
@@ -5847,9 +5849,7 @@ PHP_FUNCTION(array_multisort)
58475849
RETVAL_TRUE;
58485850

58495851
clean_up:
5850-
for (i = 0; i < array_size; i++) {
5851-
efree(indirect[i]);
5852-
}
5852+
efree(indirects);
58535853
efree(indirect);
58545854
efree(func);
58555855
efree(arrays);

0 commit comments

Comments
 (0)