summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc
diff options
context:
space:
mode:
authormillert <>2017-05-17 18:07:03 +0000
committermillert <>2017-05-17 18:07:03 +0000
commita94e19c4919aa8f62a0d1a7eea58ab1fdc3832e3 (patch)
tree7a0d5e979af959125572d09ba66238e8b56a9b45 /src/regress/lib/libc
parent2b54a8bc04e76c465705df661ebf37030e912cf7 (diff)
downloadopenbsd-a94e19c4919aa8f62a0d1a7eea58ab1fdc3832e3.tar.gz
openbsd-a94e19c4919aa8f62a0d1a7eea58ab1fdc3832e3.tar.bz2
openbsd-a94e19c4919aa8f62a0d1a7eea58ab1fdc3832e3.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.
Diffstat (limited to 'src/regress/lib/libc')
-rw-r--r--src/regress/lib/libc/qsort/qsort_test.c12
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
29enum distribution { 31enum 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 }