From bcd01cf79a73254212547e0a75db26691bc946b5 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Wed, 17 Oct 2007 21:44:36 -0700 Subject: [PATCH] Fixed malloc() issues in test. Refactored memcached_get() (looks to be around 15% faster) --- lib/memcached_get.c | 12 ++++++++---- tests/test.c | 7 +++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 251cd592..baff7074 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -133,8 +133,8 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, uint16_t *flags, memcached_return *error) { - size_t send_length; char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + char *buf_ptr= buffer; unsigned int server_key; char *value; LIBMEMCACHED_MEMCACHED_GET_START(); @@ -147,10 +147,14 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, if (*error != MEMCACHED_SUCCESS) goto error; - send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n", - (int)key_length, key); + memcpy(buf_ptr, "get ", 4); + buf_ptr+= 4; + memcpy(buf_ptr, key, key_length); + buf_ptr+= key_length; + memcpy(buf_ptr, "\r\n", 2); + buf_ptr+= 2; - if ((memcached_io_write(ptr, server_key, buffer, send_length, 1)) == -1) + if ((memcached_io_write(ptr, server_key, buffer, (size_t)(buf_ptr - buffer), 1)) == -1) { *error= MEMCACHED_WRITE_FAILURE; goto error; diff --git a/tests/test.c b/tests/test.c index 3a8c3657..0bdf760a 100644 --- a/tests/test.c +++ b/tests/test.c @@ -515,10 +515,11 @@ void user_supplied_bug1(memcached_st *memc) int size= 0; srand(time(NULL)); char key[10]; - char *randomstuff = (char *)malloc(6 * 1024); + char randomstuff[6 * 1024]; + memcached_return rc; + memset(randomstuff, 0, 6 * 1024); - memcached_return rc; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, &setter); memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, &setter); @@ -880,5 +881,7 @@ int main(int argc, char *argv[]) fprintf(stderr, "All tests completed successfully\n\n"); + memcached_server_list_free(servers); + return 0; } -- 2.30.2