Skip to content

Commit

Permalink
bsearch crash fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mumurik committed Apr 30, 2010
1 parent b923bc0 commit 022f11b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mona/core/monalibc/stdlib/bsearch.c
Expand Up @@ -14,6 +14,8 @@ void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*fn
while((result > (char*)base) && !fnc(key, result - size)) result -= size; /* search lower result */
return result;
} else if(cmp < 0){ /* key < result */
if(mid == 0)
break; /* hi becomes -1, but it's unsigned, so while condition is not enough */
hi = mid - 1;
} else { /* cmp > 0 : key > result */
lo = mid + 1;
Expand Down
42 changes: 40 additions & 2 deletions mona/test/monalibc/stdio/test.cpp
Expand Up @@ -14,6 +14,7 @@ void test_fwrite_small();
void test_fwrite_manytimes();
void test_fread_biggerthanfile();
void test_fread_small_many();
void test_bsearch();

int main(int argc, char* argv[])
{
Expand All @@ -30,6 +31,7 @@ int main(int argc, char* argv[])
test_fread_small_many();
test_fwrite_small();
test_fwrite_manytimes();
test_bsearch();
TEST_RESULTS(stdio);
return 0;
}
Expand Down Expand Up @@ -160,13 +162,49 @@ void test_fwrite_manytimes()
EXPECT_EQ(size, sizeof(expected)-1);

int i;
/*
for(i = 0; i < size; i++)
{
EXPECT_EQ(expected[i], actual[i]);
}
*/

fclose(fp);
monapi_file_delete(path);
}

static int cmp_int(const void* key, const void*target)
{
int keyi = ((int)key);
int targeti = *((int *)target);
return keyi-targeti;
}

#include <stdlib.h>

void test_bsearch()
{
int arrays[] = {
1, 3, 5, 7
};

void* actual = bsearch((void*)2, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == NULL);

actual = bsearch((void*)3, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == &arrays[1]);

actual = bsearch((void*)5, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == &arrays[2]);

actual = bsearch((void*)7, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == &arrays[3]);

actual = bsearch((void*)8, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == NULL);

actual = bsearch((void*)1, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == &arrays[0]);

// once crashed this case.
actual = bsearch((void*)0, (void*)arrays, 4, sizeof(int), cmp_int);
EXPECT_TRUE(actual == NULL);
}

0 comments on commit 022f11b

Please sign in to comment.