]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
zstd requires a stable sort. 1100/head
authorBjörn Ketelaars <bjorn.ketelaars@hydroxide.nl>
Thu, 5 Apr 2018 05:17:17 +0000 (07:17 +0200)
committerBjörn Ketelaars <bjorn.ketelaars@hydroxide.nl>
Thu, 5 Apr 2018 05:59:16 +0000 (07:59 +0200)
On OpenBSD qsort() is not guaranteed to be stable, their mergesort() is.
This fixes issue #1088. All the hard work has been done by @terrelln.

lib/dictBuilder/cover.c

index b5a3957a9b960933066b4f857647abeb1ed40cfb..6d473624d2993f42e34a79c1593fc72231b4017b 100644 (file)
@@ -581,10 +581,17 @@ static int COVER_ctx_init(COVER_ctx_t *ctx, const void *samplesBuffer,
     for (i = 0; i < ctx->suffixSize; ++i) {
       ctx->suffix[i] = i;
     }
-    /* qsort doesn't take an opaque pointer, so pass as a global */
+    /* qsort doesn't take an opaque pointer, so pass as a global.
+     * On OpenBSD qsort() is not guaranteed to be stable, their mergesort() is.
+     */
     g_ctx = ctx;
+#if defined(__OpenBSD__)
+    mergesort(ctx->suffix, ctx->suffixSize, sizeof(U32),
+          (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
+#else
     qsort(ctx->suffix, ctx->suffixSize, sizeof(U32),
           (ctx->d <= 8 ? &COVER_strict_cmp8 : &COVER_strict_cmp));
+#endif
   }
   DISPLAYLEVEL(2, "Computing frequencies\n");
   /* For each dmer group (group of positions with the same first d bytes):