diff options
author | Trond Norbye <trond.norbye@gmail.com> | 2010-11-10 06:11:24 -0800 |
---|---|---|
committer | Trond Norbye <trond.norbye@gmail.com> | 2010-11-10 06:22:01 -0800 |
commit | 16a809e2a06200a624c8742554b8943de87cc356 (patch) | |
tree | 4209f2706a7a8648d71a9122120d061cd6521dae | |
parent | df15887584f0025e7b188e408dd3c9f638d68518 (diff) | |
download | memcached-16a809e2a06200a624c8742554b8943de87cc356.tar.gz |
Issue 161 incorrect allocation in cache_create
-rw-r--r-- | cache.c | 2 | ||||
-rw-r--r-- | testapp.c | 34 |
2 files changed, 35 insertions, 1 deletions
@@ -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); @@ -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 }, |