[net.bugs.usg] bug in lfind

jr@foros1.UUCP (John Rogers) (06/27/84)

Subject: bug in lfind() - man page doesn't match code

Index: lfind() /usr/src/lib/libc/port/gen/lfind.c Sys5.2

Description:
	The man page I have for "lfind" (a linear search routine,
	documented in "lsearch(3C)") indicates that lfind will return a
	NULL pointer if the datum is not found in the table.  In
	reality, it returns -1 cast to a pointer.

	This only applies to System V Release 2; the first release
	doesn't have lfind.  It's possible that other versions of the
	man page document lfind as returning -1; I only have the one
	printed in "UNIX System V Release 2.0 Programmer Reference
	Manual - DEC Processors".

Repeat-By:
	Run the following program:
-----------------------------cut here------------------------------
#include <stdio.h>
extern char *lfind();
extern char *lsearch();
extern int strcmp();

main() {
	char *ptr;
	char table[100];
	unsigned nel;		/* Number of elements in table. */
	(void) lsearch("foo", table, &nel, 4, strcmp);
	(void) lsearch("bar", table, &nel, 4, strcmp);

	ptr = lfind("xxx", table, &nel, 4, strcmp);
	if (ptr == (char *)NULL)
		(void) printf("This should print, but doesn't.\n");
	else if (ptr == (char *)-1)
		(void) printf("This shouldn't print, but does.\n");
}
------------------------cut here--------------------------------

Fix:
	Make the following changes to lfind.c:
diff -rs lfind.c- lfind.c
13a14,16
>  *
>  * jr  840627 - Changed to match the documentation, i.e return NULL rather
>  *		than -1 if the element can't be found.
15a19,20
> #include <stdio.h>
> 
32c37
< 	return (POINTER)(-1);
---
> 	return ( (POINTER) NULL );


-- 
				JR (John Rogers)
				...ihnp4!fortune!foros1!jr
				also fortune!jr and proper!jr