summaryrefslogtreecommitdiffstats
path: root/lib/libc/stdlib/qsort.c
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2010-02-08 11:04:07 +0000
committerotto <otto@openbsd.org>2010-02-08 11:04:07 +0000
commitb95a9429c83613880449cab4b191998d15f86fa5 (patch)
tree41026479fd53f107184ac81778ad2a2b171d1df0 /lib/libc/stdlib/qsort.c
parentuse size_t to index arrays; avoids big array bugs; ok millert@ guenther@ (diff)
downloadwireguard-openbsd-b95a9429c83613880449cab4b191998d15f86fa5.tar.xz
wireguard-openbsd-b95a9429c83613880449cab4b191998d15f86fa5.zip
Use size_t in appropriate places; fixes sorting of big arrays;
after the diff was written, I made it similar to the freebsd fix of the same code; pr6287 ok millert@ guenther@
Diffstat (limited to 'lib/libc/stdlib/qsort.c')
-rw-r--r--lib/libc/stdlib/qsort.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/lib/libc/stdlib/qsort.c b/lib/libc/stdlib/qsort.c
index bb4a9a11f2b..f28449fb5bf 100644
--- a/lib/libc/stdlib/qsort.c
+++ b/lib/libc/stdlib/qsort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */
+/* $OpenBSD: qsort.c,v 1.11 2010/02/08 11:04:07 otto Exp $ */
/*-
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
@@ -32,7 +32,7 @@
#include <stdlib.h>
static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *));
-static __inline void swapfunc(char *, char *, int, int);
+static __inline void swapfunc(char *, char *, size_t, int);
#define min(a, b) (a) < (b) ? a : b
@@ -40,7 +40,7 @@ static __inline void swapfunc(char *, char *, int, int);
* Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
*/
#define swapcode(TYPE, parmi, parmj, n) { \
- long i = (n) / sizeof (TYPE); \
+ size_t i = (n) / sizeof (TYPE); \
TYPE *pi = (TYPE *) (parmi); \
TYPE *pj = (TYPE *) (parmj); \
do { \
@@ -54,7 +54,7 @@ static __inline void swapfunc(char *, char *, int, int);
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
static __inline void
-swapfunc(char *a, char *b, int n, int swaptype)
+swapfunc(char *a, char *b, size_t n, int swaptype)
{
if (swaptype <= 1)
swapcode(long, a, b, n)
@@ -84,7 +84,8 @@ void
qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *))
{
char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
- int d, r, swaptype, swap_cnt;
+ int cmp_result, swaptype, swap_cnt;
+ size_t d, r;
char *a = aa;
loop: SWAPINIT(a, es);
@@ -113,16 +114,16 @@ loop: SWAPINIT(a, es);
pc = pd = (char *)a + (n - 1) * es;
for (;;) {
- while (pb <= pc && (r = cmp(pb, a)) <= 0) {
- if (r == 0) {
+ while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) {
+ if (cmp_result == 0) {
swap_cnt = 1;
swap(pa, pb);
pa += es;
}
pb += es;
}
- while (pb <= pc && (r = cmp(pc, a)) >= 0) {
- if (r == 0) {
+ while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) {
+ if (cmp_result == 0) {
swap_cnt = 1;
swap(pc, pd);
pd -= es;