You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
array_unique assertion failure with RC1 array causing an exception on sort #20043
Interesting. The reason this happens is because the array_unique operation happens in-place because the input array is RC1. At one point during comparison an exception is thrown which will capture the arguments in the backtrace, which will increment the refcount of the RC1 array to 2. Then a modification happens after the throw on the RC2 array causing the assertion failure. Arguably we shouldn't try continue work after an exception happened during the sort:
diff --git a/ext/standard/array.c b/ext/standard/array.c
index d708a339c6b..b6d81193ed6 100644
--- a/ext/standard/array.c+++ b/ext/standard/array.c@@ -5044,6 +5044,9 @@ PHP_FUNCTION(array_unique)
ZVAL_UNDEF(&arTmp[i].b.val);
zend_sort((void *) arTmp, i, sizeof(struct bucketindex),
(compare_func_t) cmp, (swap_func_t) array_bucketindex_swap);
+ if (UNEXPECTED(EG(exception))) {+ goto out;+ }
/* go through the sorted array and delete duplicates from the copy */
lastkept = arTmp;
for (cmpdata = arTmp + 1; Z_TYPE(cmpdata->b.val) != IS_UNDEF; cmpdata++) {
@@ -5063,6 +5066,7 @@ PHP_FUNCTION(array_unique)
}
}
}
+out:
pefree(arTmp, GC_FLAGS(Z_ARRVAL_P(array)) & IS_ARRAY_PERSISTENT);
if (in_place) {
This patch fixes it but it's a bit ugly (and an extra check :-().
changed the title [-]assertion failure zend_hash_del[/-][+]array_unique assertion failure with RC1 array causing an exception on sort[/+]on Oct 4, 2025
Activity
ndossche commentedon Oct 3, 2025
Interesting. The reason this happens is because the array_unique operation happens in-place because the input array is RC1. At one point during comparison an exception is thrown which will capture the arguments in the backtrace, which will increment the refcount of the RC1 array to 2. Then a modification happens after the throw on the RC2 array causing the assertion failure. Arguably we shouldn't try continue work after an exception happened during the sort:
This patch fixes it but it's a bit ugly (and an extra check :-().
[-]assertion failure zend_hash_del[/-][+]array_unique assertion failure with RC1 array causing an exception on sort[/+]Fix phpGH-20043: array_unique assertion failure with RC1 array causin…
Fix GH-20043: array_unique assertion failure with RC1 array causing a…
Merge branch 'PHP-8.3' into PHP-8.4
Merge branch 'PHP-8.4' into PHP-8.5
Merge branch 'PHP-8.5'
fix(standard): guard the in-place optimization against reentrant dest…
fix(standard): guard the in-place optimization against reentrant dest…