summaryrefslogtreecommitdiffstats
path: root/lib/libc/net/name6.c
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2016-09-03 01:09:22 +0000
committerache <ache@FreeBSD.org>2016-09-03 01:09:22 +0000
commit833d85cdfebda1a68898dd0b713a501551739080 (patch)
tree7b1d089602bdd0649369fde9954b550dbb832ec1 /lib/libc/net/name6.c
parentfd53a9f649c533eae7ac796a6ca1226b11c594be (diff)
downloadFreeBSD-src-833d85cdfebda1a68898dd0b713a501551739080.zip
FreeBSD-src-833d85cdfebda1a68898dd0b713a501551739080.tar.gz
MFC r305133
Apply the same qsort() usage fix as in r304911 getaddrinfo.c qsort() can't be stabilized with just return(-1) alone.
Diffstat (limited to 'lib/libc/net/name6.c')
-rw-r--r--lib/libc/net/name6.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/libc/net/name6.c b/lib/libc/net/name6.c
index 90b4562..7e52a51 100644
--- a/lib/libc/net/name6.c
+++ b/lib/libc/net/name6.c
@@ -185,6 +185,7 @@ struct hp_order {
#define aio_sa aio_un.aiou_sa
int aio_matchlen;
char *aio_h_addr;
+ int aio_initial_sequence;
};
static struct hostent *_hpcopy(struct hostent *, int *);
@@ -711,6 +712,7 @@ _hpreorder(struct hostent *hp)
aio[i].aio_dstscope = gai_addr2scopetype(sa);
aio[i].aio_dstpolicy = match_addrselectpolicy(sa, &policyhead);
set_source(&aio[i], &policyhead);
+ aio[i].aio_initial_sequence = i;
}
/* perform sorting. */
@@ -1045,6 +1047,23 @@ comp_dst(const void *arg1, const void *arg2)
}
/* Rule 10: Otherwise, leave the order unchanged. */
+
+ /*
+ * Note that qsort is unstable; so, we can't return zero and
+ * expect the order to be unchanged.
+ * That also means we can't depend on the current position of
+ * dst2 being after dst1. We must enforce the initial order
+ * with an explicit compare on the original position.
+ * The qsort specification requires that "When the same objects
+ * (consisting of width bytes, irrespective of their current
+ * positions in the array) are passed more than once to the
+ * comparison function, the results shall be consistent with one
+ * another."
+ * In other words, If A < B, then we must also return B > A.
+ */
+ if (dst2->aio_initial_sequence < dst1->aio_initial_sequence)
+ return(1);
+
return(-1);
}
OpenPOWER on IntegriCloud