]> granicus.if.org Git - php/commitdiff
Forbid exchangeArray() during sorting
authorNikita Popov <nikic@php.net>
Wed, 24 Feb 2016 17:27:56 +0000 (18:27 +0100)
committerNikita Popov <nikic@php.net>
Wed, 24 Feb 2016 21:34:50 +0000 (22:34 +0100)
Previously this would leak.

ext/spl/spl_array.c
ext/spl/tests/ArrayObject_exchange_array_during_sorting.phpt [new file with mode: 0644]

index 0740c063e961b45b9012b6e6c0f8eff87c91c9b8..2de985bd224c407f32470ec2a762d29ab27579f9 100644 (file)
@@ -1267,6 +1267,11 @@ SPL_METHOD(Array, exchangeArray)
                return;
        }
 
+       if (intern->nApplyCount > 0) {
+               zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
+               return;
+       }
+
        RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
        spl_array_set_array(object, intern, array, 0L, 1);
 }
diff --git a/ext/spl/tests/ArrayObject_exchange_array_during_sorting.phpt b/ext/spl/tests/ArrayObject_exchange_array_during_sorting.phpt
new file mode 100644 (file)
index 0000000..225d42c
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Can't use exchangeArray() while ArrayObject is being sorted
+--FILE--
+<?php
+
+$ao = new ArrayObject([1, 2, 3]);
+$i = 0;
+$ao->uasort(function($a, $b) use ($ao, &$i) {
+    if ($i++ == 0) {
+        $ao->exchangeArray([4, 5, 6]);
+        var_dump($ao);
+    }
+    return $a <=> $b;
+});
+
+?>
+--EXPECTF--
+Warning: Modification of ArrayObject during sorting is prohibited in %s on line %d
+object(ArrayObject)#1 (1) {
+  ["storage":"ArrayObject":private]=>
+  array(3) {
+    [0]=>
+    int(1)
+    [1]=>
+    int(2)
+    [2]=>
+    int(3)
+  }
+}