This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

nasty bug in bsearch() in b17.1 under Windows'95????


I believe there is a nasty bug in bsearch that gets tickled whenever the
item to be searched for is "greater" than the last item in the list
supplied. Take the following trivial code:

=== bsearch-bug.c

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *mynames[] = {"bb", "cc", "za"};

#define ARRAY_SIZE(x) sizeof(x)/sizeof((x)[0])

static int compare_name(const void *name, const void *item_name) {
    const char *x_name = (char*) name;
    const char *y_name = *((char**) item_name);
    /* in gdb, do a display on both x_name and y_name */
    return strcmp(x_name, y_name);
}

int main(int argc, char* argv[]) {
    char *what = (argc == 2) ? argv[1] : "zb";
    char *item = bsearch(what, &mynames[0], ARRAY_SIZE (mynames),
        sizeof (mynames[0]), (void*) compare_name);
    printf ("Item %s %s\n", what, (item) ? "FOUND" : "NOT FOUND");
    return 0;
}

=== bsearch-bug.c

% gcc -g -o bsearch-bug.exe bsearch.c
% ./bsearch aa
Item aa NOT FOUND
% ./bsearch bb
Item bb FOUND
% ./bsearch za
Item za FOUND
% ./bsearch zb
Item zb NOT FOUND		<<- this is expected, but it's buggy!

The last case is where the bug is. The comparison function is passed a
pointer that is *one past* the last item. To see it, simply use the
display feature in gdb and watch for the addresses being passed to
compare_name when trying to find "zb".

Could someone please verify this on their system? Mine is rather hacked
up and I would like a second opinion before calling it a bug.
Unfortunately I'm on the road with no ready access to the source tree,
but hopefully will post a fix when I get back in a day or two (unless
of course the bug is due to my own changes :-).

fyi, this breaks g77 quite horribly when compiling anything with
a subroutine or function name of "zsw" or greater.

Cheers
Mumit -- khan@xraylith.wisc.edu
http://www.xraylith.wisc.edu/~khan/
-
For help on using this list, send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]