Hi,
On Fri, 5 Dec 2003, Tom Lane wrote:
> > Hm, Solaris' bsearch() fails on empty input? How bizarre.
>
> I was skeptical but apparently this is a known bug ...
> googling turned up a couple of references, eg
> http://www.opencm.org/pipermail/opencm-dev/2002-July/001077.html
in defense of Solaris' bsearch it should be said that it only breaks if
one passes NULL as the array base, a decidedly undefined (and unfriendly)
case. Passing an array with zero elements works as advertised. Btw, the
same happens on IRIX.
Best wishes, Mike
PS: A little program to demonstrate this: array with elements, empty array, NULL pointer as base:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char strings[][4] = { "abc", "efg", "hij", "klm" };
typedef int (*cmp_t)(const void*, const void*);
int main(int argc, char**argv) { char *s, *term = "hij";
s = bsearch(term, strings, sizeof(strings)/sizeof(char[4]), sizeof(char*), (cmp_t) strcmp);
fprintf(stderr,"1: %s\n", (s != NULL) ? s : "<not found>");
s = bsearch(term, strings, 0, sizeof(char*), (cmp_t) strcmp); fprintf(stderr, "2: %s\n", (s != NULL) ? s : "<not
found>");
s = bsearch(term, NULL, 0, sizeof(char*), (cmp_t) strcmp); fprintf(stderr, "3: %s\n", (s != NULL) ? s : "<not
found>");
return 0;
}
Results:
$ ./a.out # Solaris 9
1: hij
2: <not found>
Segmentation Fault (core dumped)
$ ./a.out # IRIX 6.5
1: hij
2: <not found>
Segmentation fault (core dumped)
$ ./a.out # Linux with glibc 2.2.5
1: hij
2: <not found>
3: <not found>
$ ./a.out # OpenBSD 3.2
1: hij
2: <not found>
3: <not found>
--
Life is like a fire. DI Michael Wildpaner
Flames which the passer-by forgets. Ph.D. Student
Ashes which the wind scatters.
A man lived. -- Omar Khayyam