summaryrefslogtreecommitdiffstats
path: root/regress/lib/libc/qsort
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2017-05-17 18:07:03 +0000
committermillert <millert@openbsd.org>2017-05-17 18:07:03 +0000
commit4a3e92176d795c633db412b781c87995dbf60aab (patch)
tree5fed56d5197e97ce5c0510faaa11eb70aa46465c /regress/lib/libc/qsort
parentRevert MI AES-XTS code back to T-tables amid poor performance (diff)
downloadwireguard-openbsd-4a3e92176d795c633db412b781c87995dbf60aab.tar.xz
wireguard-openbsd-4a3e92176d795c633db412b781c87995dbf60aab.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 'regress/lib/libc/qsort')
-rw-r--r--regress/lib/libc/qsort/qsort_test.c12
1 files changed, 12 insertions, 0 deletions
diff --git a/regress/lib/libc/qsort/qsort_test.c b/regress/lib/libc/qsort/qsort_test.c
index f78137504a2..b23f3e61905 100644
--- a/regress/lib/libc/qsort/qsort_test.c
+++ b/regress/lib/libc/qsort/qsort_test.c
@@ -24,6 +24,8 @@
/*
* Test program based on Bentley & McIlroy's "Engineering a Sort Function".
* Uses heapsort(3) to check the results.
+ * The "killer" input is from:
+ * http://calmerthanyouare.org/2014/06/11/algorithmic-complexity-attacks-and-libc-qsort.html
*/
enum distribution {
@@ -32,6 +34,7 @@ enum distribution {
STAGGER,
PLATEAU,
SHUFFLE,
+ KILLER,
INVALID
};
@@ -106,6 +109,15 @@ fill_test_array(int *x, int n, int dist, int m)
case SHUFFLE:
x[i] = arc4random_uniform(m) ? (j += 2) : (k += 2);
break;
+ case KILLER:
+ k = n / 2;
+ if (i < k)
+ x[i] = k - i;
+ else if (i > k)
+ x[i] = n + k + 1 - i;
+ else
+ x[i] = k + 1;
+ break;
default:
err(1, "unexpected distribution %d", dist);
}