Skip to content

Commit 18a0d46

Browse files
committed
Safely reassign array in usort()
Make sure to destroy the old value only after assigning the new one. Otherwise we may try to double free, e.g. if GC runs during this dtor. This caused an assertion failure in phpro/grumphp and is likely the cause for bug #81603 as well. (cherry picked from commit 6f38acf) (I applied this to the wrong base branch at first...)
1 parent 45f5228 commit 18a0d46

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

ext/standard/array.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,10 @@ static void php_usort(INTERNAL_FUNCTION_PARAMETERS, compare_func_t compare_func,
10371037

10381038
retval = zend_hash_sort(arr, compare_func, renumber) != FAILURE;
10391039

1040-
zval_ptr_dtor(array);
1040+
zval garbage;
1041+
ZVAL_COPY_VALUE(&garbage, array);
10411042
ZVAL_ARR(array, arr);
1043+
zval_ptr_dtor(&garbage);
10421044

10431045
PHP_ARRAY_CMP_FUNC_RESTORE();
10441046
RETURN_BOOL(retval);

0 commit comments

Comments
 (0)