[uClibc]bug#1099: bsearch isn't SUSv2 compliant

Matt Kraai kraai at alumni.carnegiemellon.edu
Thu Feb 1 17:57:21 UTC 2001


Package: uClibc
Version: CVS
Tags: patch

According to the Single Unix Specification version 2 manual page
for bsearch,

	The comparison function pointed to by compar is called
	with two arguments that point to the key object and to an
	array element, in that order.

In uClibc, however, the compar function is always called with the
key as the second argument.  The appended patch changes the
argument order so that it complies with the specification (and,
from testing, with glibc).

Matt

Index: stdlib/bsearch.c
===================================================================
RCS file: /var/cvs/uClibc/stdlib/bsearch.c,v
retrieving revision 1.3
diff -u -r1.3 bsearch.c
--- stdlib/bsearch.c	2000/10/09 20:06:14	1.3
+++ stdlib/bsearch.c	2001/02/01 17:43:53
@@ -27,10 +27,10 @@
 	b = num - 1;
 	while (a <= b) {
 		c = (a + b) >> 1;		/* == ((a + b) / 2) */
-		if ((dir = (*cmp) ((base + (c * size)), key))) {
-			if (dir > 0)
+		if ((dir = (*cmp) (key, (base + (c * size))))) {
+			if (dir < 0)
 				b = c - 1;
-			else				/* (dir < 0) */
+			else				/* (dir > 0) */
 				a = c + 1;
 		} else {
 			_bsearch = c;






More information about the uClibc mailing list