Skip to content

Fix instable array during in-place modification in uksort #13285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Zend/tests/gh13279.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this should go to ext/standard/tests/array

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. TBH the separation of these tests is often quite arbitrary. I usually just throw stuff in Zend.

GH-13279: Instable array during in-place modification in uksort
--FILE--
<?php

// Make sure the array is not const
$array = [];
$array['a'] = 1;
$array['b'] = 2;

uksort($array, function ($a, $b) use (&$array) {
return $array[$a] - $array[$b];
});

?>
===DONE===
--EXPECT--
===DONE===
12 changes: 2 additions & 10 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,19 +901,11 @@ static void php_usort(INTERNAL_FUNCTION_PARAMETERS, bucket_compare_func_t compar
RETURN_TRUE;
}

/* Copy array, so the in-place modifications will not be visible to the callback function.
* Unless there are no other references since we know for sure it won't be visible. */
bool in_place = zend_may_modify_arg_in_place(array);
if (!in_place) {
arr = zend_array_dup(arr);
}
/* Copy array, so the in-place modifications will not be visible to the callback function */
arr = zend_array_dup(arr);

zend_hash_sort(arr, compare_func, renumber);

if (in_place) {
GC_ADDREF(arr);
}

zval garbage;
ZVAL_COPY_VALUE(&garbage, array);
ZVAL_ARR(array, arr);
Expand Down