mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Karl Palsson <karlp@tweak.net.au>
To: musl@lists.openwall.com
Cc: Karl Palsson <karlp@remake.is>, Karl Palsson <karlp@etactica.com>
Subject: [PATCH] search: call user compare with "correct" order params
Date: Wed, 24 Feb 2016 12:12:29 +0000	[thread overview]
Message-ID: <1456315949-16347-1-git-send-email-karlp@tweak.net.au> (raw)

From: Karl Palsson <karlp@remake.is>

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 <karlp@etactica.com>
---
 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;
 }
-- 
2.4.3



             reply	other threads:[~2016-02-24 12:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-24 12:12 Karl Palsson [this message]
2016-02-24 17:41 ` Rich Felker
2016-03-08 11:43   ` Karl Pálsson
2016-03-08 17:24     ` dalias

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1456315949-16347-1-git-send-email-karlp@tweak.net.au \
    --to=karlp@tweak.net.au \
    --cc=karlp@etactica.com \
    --cc=karlp@remake.is \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).