diff options
author | millert <> | 2017-05-17 18:07:03 +0000 |
---|---|---|
committer | millert <> | 2017-05-17 18:07:03 +0000 |
commit | c064c4b6dfffe8755611fa1bdd53f182b8aa8221 (patch) | |
tree | 7a0d5e979af959125572d09ba66238e8b56a9b45 | |
parent | fa9ca80852b3b1ef830abad923797c4a84b10a9e (diff) | |
download | openbsd-c064c4b6dfffe8755611fa1bdd53f182b8aa8221.tar.gz openbsd-c064c4b6dfffe8755611fa1bdd53f182b8aa8221.tar.bz2 openbsd-c064c4b6dfffe8755611fa1bdd53f182b8aa8221.zip |
Add "killer" input from "algorithmic complexity attacks and libc
qsort()". This causes quadratic behavior with the 4.4BSD qsort's
"switch to insertion sort" optimization when the input appears to
be mostly sorted. That optimization was removed in qsort.c r1.12
but it is worth having in the regress test too.
-rw-r--r-- | src/regress/lib/libc/qsort/qsort_test.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/regress/lib/libc/qsort/qsort_test.c b/src/regress/lib/libc/qsort/qsort_test.c index f78137504a..b23f3e6190 100644 --- a/src/regress/lib/libc/qsort/qsort_test.c +++ b/src/regress/lib/libc/qsort/qsort_test.c | |||
@@ -24,6 +24,8 @@ | |||
24 | /* | 24 | /* |
25 | * Test program based on Bentley & McIlroy's "Engineering a Sort Function". | 25 | * Test program based on Bentley & McIlroy's "Engineering a Sort Function". |
26 | * Uses heapsort(3) to check the results. | 26 | * Uses heapsort(3) to check the results. |
27 | * The "killer" input is from: | ||
28 | * http://calmerthanyouare.org/2014/06/11/algorithmic-complexity-attacks-and-libc-qsort.html | ||
27 | */ | 29 | */ |
28 | 30 | ||
29 | enum distribution { | 31 | enum distribution { |
@@ -32,6 +34,7 @@ enum distribution { | |||
32 | STAGGER, | 34 | STAGGER, |
33 | PLATEAU, | 35 | PLATEAU, |
34 | SHUFFLE, | 36 | SHUFFLE, |
37 | KILLER, | ||
35 | INVALID | 38 | INVALID |
36 | }; | 39 | }; |
37 | 40 | ||
@@ -106,6 +109,15 @@ fill_test_array(int *x, int n, int dist, int m) | |||
106 | case SHUFFLE: | 109 | case SHUFFLE: |
107 | x[i] = arc4random_uniform(m) ? (j += 2) : (k += 2); | 110 | x[i] = arc4random_uniform(m) ? (j += 2) : (k += 2); |
108 | break; | 111 | break; |
112 | case KILLER: | ||
113 | k = n / 2; | ||
114 | if (i < k) | ||
115 | x[i] = k - i; | ||
116 | else if (i > k) | ||
117 | x[i] = n + k + 1 - i; | ||
118 | else | ||
119 | x[i] = k + 1; | ||
120 | break; | ||
109 | default: | 121 | default: |
110 | err(1, "unexpected distribution %d", dist); | 122 | err(1, "unexpected distribution %d", dist); |
111 | } | 123 | } |