Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Wed, 24 Feb 2016 12:12:29 +0000
From: Karl Palsson <>
Cc: Karl Palsson <>,
	Karl Palsson <>
Subject: [PATCH] search: call user compare with "correct" order params

From: Karl Palsson <>

IEEE Std 1003.1, 2013 Edition only defines the two params to the
user callback as, "The compar argument points to a comparison function
which the application shall supply (for example, strcmp()). It is called
with two arguments that point to the elements being compared."

Both uclibc and glibc provide the arguments as, "
The comparison function referenced by compar is expected to have two
arguments which point to the key object and to an array member, in that order

Musl currently provides the arguments as array member, then key object.
While this is strictly compliant with the standard, it's equally
compliant to have the parameters in the other order.  If you are using
lfind to search a list of complex structures where the key is not the
same type as each entry, having these parameters arrive in unexpectd
order can/will result in segfaults.

=> Swap the order of the arguments to the user function, maintaining
equal compatibility with the standard, and gaining compatibility with
uclibc and glibc.

Signed-off-by: Karl Palsson <>
 src/search/lsearch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Please CC me on replies, I'm not (currently) a subscriber

diff --git a/src/search/lsearch.c b/src/search/lsearch.c
index 63f3192..5eb5cc2 100644
--- a/src/search/lsearch.c
+++ b/src/search/lsearch.c
@@ -9,7 +9,7 @@ void *lsearch(const void *key, void *base, size_t *nelp, size_t width,
 	size_t i;
 	for (i = 0; i < n; i++)
-		if (compar(p[i], key) == 0)
+		if (compar(key, p[i]) == 0)
 			return p[i];
 	*nelp = n+1;
 	return memcpy(p[n], key, width);
@@ -23,7 +23,7 @@ void *lfind(const void *key, const void *base, size_t *nelp,
 	size_t i;
 	for (i = 0; i < n; i++)
-		if (compar(p[i], key) == 0)
+		if (compar(key, p[i]) == 0)
 			return p[i];
 	return 0;

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.