]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
fix merge_collapse to actually maintain the invariant it purports to (closes #23515)
authorBenjamin Peterson <benjamin@python.org>
Wed, 25 Feb 2015 15:12:26 +0000 (10:12 -0500)
committerBenjamin Peterson <benjamin@python.org>
Wed, 25 Feb 2015 15:12:26 +0000 (10:12 -0500)
See
de Gouw, Stijn and Rot, Jurriaan and de Boer, Frank S and Bubel, Richard and Hähnle, Reiner
"OpenJDK’s java.utils.Collection.sort() is broken: The good, the bad and the worst case"

Objects/listobject.c

index f753643bd39a94987be327c791dea4eed6183cae..1f43ba292c394d3452f9ef51a34dbc4671d2f609 100644 (file)
@@ -1800,7 +1800,8 @@ merge_collapse(MergeState *ms)
     assert(ms);
     while (ms->n > 1) {
         Py_ssize_t n = ms->n - 2;
-        if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
+        if ((n > 0 && p[n-1].len <= p[n].len + p[n+1].len) ||
+            (n > 1 && p[n-2].len <= p[n-1].len + p[n].len)) {
             if (p[n-1].len < p[n+1].len)
                 --n;
             if (merge_at(ms, n) < 0)