]> granicus.if.org Git - php/commitdiff
Don't use >= as sorting condition
authorXinchen Hui <laruence@php.net>
Mon, 19 Jan 2015 06:04:23 +0000 (01:04 -0500)
committerXinchen Hui <laruence@php.net>
Mon, 19 Jan 2015 06:36:56 +0000 (01:36 -0500)
which could avoid breaking usage like:

usort($a, function($a, $b) { return $a > $b; })

Zend/zend_sort.c
ext/standard/tests/array/usort_variation11.phpt [new file with mode: 0644]

index 383fe226b408520eb6d8c555ed61480dc7f22433..78ed0ebb5fc9badf9c7d5d78293b0f292c7400b2 100644 (file)
@@ -91,10 +91,9 @@ ZEND_API void zend_qsort(void *base, size_t nmemb, size_t siz, compare_func_t co
 /* }}} */
 
 static inline void zend_sort_2(void *a, void *b, compare_func_t cmp, swap_func_t swp) /* {{{ */ {
-       if (cmp(a, b) <= 0) {
-               return;
+       if (cmp(a, b) > 0) {
+               swp(a, b);
        }
-       swp(a, b);
 }
 /* }}} */
 
@@ -109,7 +108,7 @@ static inline void zend_sort_3(void *a, void *b, void *c, compare_func_t cmp, sw
                }
                return;
        }
-       if (cmp(b, c) >= 0) {
+       if (cmp(c, b) <= 0) {
                swp(a, c);
                return;
        }
@@ -215,7 +214,7 @@ ZEND_API void zend_insert_sort(void *base, size_t nmemb, size_t siz, compare_fun
                                                }
                                                if (j == start + siz) {
                                                        j -= siz;
-                                                       if (cmp(j, i) < 0) {
+                                                       if (cmp(i, j) > 0) {
                                                                j += siz;
                                                        }
                                                        break;
@@ -338,7 +337,7 @@ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp
                        i = pivot + siz;
                        j = end - siz;
                        while (1) {
-                               while (cmp(i, pivot) < 0) {
+                               while (cmp(pivot, i) > 0) {
                                        i += siz;
                                        if (UNEXPECTED(i == j)) {
                                                goto done;
@@ -348,7 +347,7 @@ ZEND_API void zend_sort(void *base, size_t nmemb, size_t siz, compare_func_t cmp
                                if (UNEXPECTED(j == i)) {
                                        goto done;
                                }
-                               while (cmp(pivot, j) < 0) {
+                               while (cmp(j, pivot) > 0) {
                                        j -= siz;
                                        if (UNEXPECTED(j == i)) {
                                                goto done;
diff --git a/ext/standard/tests/array/usort_variation11.phpt b/ext/standard/tests/array/usort_variation11.phpt
new file mode 100644 (file)
index 0000000..b6cdeef
--- /dev/null
@@ -0,0 +1,25 @@
+--TEST--
+Test usort() function : usage variations - binary return cmp
+--FILE--
+<?php
+
+function ucmp($a, $b) {
+       return $a > $b;
+}
+
+$range = array(2, 4, 8, 16, 32, 64, 128);
+
+foreach ($range as $r) {
+       $backup = $array = range(0, $r);
+       shuffle($array);
+       usort($array, "ucmp");
+       if ($array != $backup) {
+               var_dump($array);
+               var_dump($backup);
+               die("Whatever sorting algo you used, this test should never be broken");
+       }
+}
+echo "okey";
+?>
+--EXPECT--
+okey