Skip to content

Commit

Permalink
Issue 161 incorrect allocation in cache_create
Browse files Browse the repository at this point in the history
  • Loading branch information
trondn committed Nov 10, 2010
1 parent df15887 commit 16a809e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cache.c
Expand Up @@ -21,7 +21,7 @@ cache_t* cache_create(const char *name, size_t bufsize, size_t align,
cache_destructor_t* destructor) {
cache_t* ret = calloc(1, sizeof(cache_t));
char* nm = strdup(name);
void** ptr = calloc(initial_pool_size, bufsize);
void** ptr = calloc(initial_pool_size, sizeof(void*));
if (ret == NULL || nm == NULL || ptr == NULL ||
pthread_mutex_init(&ret->mutex, NULL) == -1) {
free(ret);
Expand Down
34 changes: 34 additions & 0 deletions testapp.c
Expand Up @@ -114,6 +114,39 @@ static enum test_return cache_reuse_test(void)
return TEST_PASS;
}


static enum test_return cache_bulkalloc(size_t datasize)
{
cache_t *cache = cache_create("test", datasize, sizeof(char*),
NULL, NULL);
#define ITERATIONS 1024
void *ptr[ITERATIONS];

for (int ii = 0; ii < ITERATIONS; ++ii) {
ptr[ii] = cache_alloc(cache);
assert(ptr[ii] != 0);
memset(ptr[ii], 0xff, datasize);
}

for (int ii = 0; ii < ITERATIONS; ++ii) {
cache_free(cache, ptr[ii]);
}

#undef ITERATIONS
cache_destroy(cache);
return TEST_PASS;
}

static enum test_return test_issue_161(void)
{
enum test_return ret = cache_bulkalloc(1);
if (ret == TEST_PASS) {
ret = cache_bulkalloc(512);
}

return ret;
}

static enum test_return cache_redzone_test(void)
{
#ifndef HAVE_UMEM_H
Expand Down Expand Up @@ -1786,6 +1819,7 @@ struct testcase testcases[] = {
{ "cache_destructor", cache_destructor_test },
{ "cache_reuse", cache_reuse_test },
{ "cache_redzone", cache_redzone_test },
{ "issue_161", test_issue_161 },
{ "strtol", test_safe_strtol },
{ "strtoll", test_safe_strtoll },
{ "strtoul", test_safe_strtoul },
Expand Down

0 comments on commit 16a809e

Please sign in to comment.