Skip to content
Browse files

Issue 161 incorrect allocation in cache_create

  • Loading branch information...
1 parent df15887 commit 16a809e2a06200a624c8742554b8943de87cc356 @trondn committed Nov 10, 2010
Showing with 35 additions and 1 deletion.
  1. +1 −1 cache.c
  2. +34 −0 testapp.c
View
2 cache.c
@@ -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);
View
34 testapp.c
@@ -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
@@ -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 },

0 comments on commit 16a809e

Please sign in to comment.
Something went wrong with that request. Please try again.