From 6ebad5d9496eef6c2f1ad843198bb0413be6e16f Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 17 Jun 2011 16:22:47 -0700 Subject: [PATCH] Just hand over memory ownership when the caller is supposed to free it (we have before this made a copy and used the previous as a buffer). --- libmemcached/fetch.cc | 2 +- libmemcached/get.cc | 3 +-- libmemcached/string.cc | 29 +++++++++++++++++++++++++++++ libmemcached/string.h | 3 +++ tests/mem_functions.cc | 18 ++++++++++-------- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/libmemcached/fetch.cc b/libmemcached/fetch.cc index 5a353d29..e947ddb2 100644 --- a/libmemcached/fetch.cc +++ b/libmemcached/fetch.cc @@ -115,7 +115,7 @@ char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length, if (flags) *flags= result_buffer->item_flags; - return memcached_string_c_copy(&result_buffer->value); + return memcached_string_take_value(&result_buffer->value); } memcached_result_st *memcached_fetch_result(memcached_st *ptr, diff --git a/libmemcached/get.cc b/libmemcached/get.cc index bcc468a0..b95aa0c1 100644 --- a/libmemcached/get.cc +++ b/libmemcached/get.cc @@ -111,7 +111,6 @@ char *memcached_get_by_key(memcached_st *ptr, { if (ptr->get_key_failure && *error == MEMCACHED_NOTFOUND) { - memcached_result_reset(&ptr->result); memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result); @@ -148,7 +147,7 @@ char *memcached_get_by_key(memcached_st *ptr, *error= rc; *value_length= memcached_result_length(&ptr->result); *flags= memcached_result_flags(&ptr->result); - return memcached_string_c_copy(&ptr->result.value); + return memcached_string_take_value(&ptr->result.value); } } } diff --git a/libmemcached/string.cc b/libmemcached/string.cc index 9e029627..6f3b9c1c 100644 --- a/libmemcached/string.cc +++ b/libmemcached/string.cc @@ -37,6 +37,7 @@ #include +#include inline static memcached_return_t _string_check(memcached_string_st *string, size_t need) { @@ -118,6 +119,18 @@ memcached_string_st *memcached_string_create(memcached_st *memc, memcached_strin return self; } +static memcached_return_t memcached_string_append_null(memcached_string_st *string) +{ + if (memcached_failed(_string_check(string, 1))) + { + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } + + *string->end= 0; + + return MEMCACHED_SUCCESS; +} + memcached_return_t memcached_string_append_character(memcached_string_st *string, char character) { @@ -213,6 +226,22 @@ const char *memcached_string_value(const memcached_string_st *self) return self->string; } +char *memcached_string_take_value(memcached_string_st *self) +{ + assert(self); + // If we fail at adding the null, we copy and move on + if (memcached_success(memcached_string_append_null(self))) + { + return memcached_string_c_copy(self); + } + + char *value= self->string; + + _init_string(self); + + return value; +} + char *memcached_string_value_mutable(const memcached_string_st *self) { return self->string; diff --git a/libmemcached/string.h b/libmemcached/string.h index a0b66ed3..3a3fdf53 100644 --- a/libmemcached/string.h +++ b/libmemcached/string.h @@ -97,6 +97,9 @@ size_t memcached_string_size(const memcached_string_st *self); LIBMEMCACHED_LOCAL const char *memcached_string_value(const memcached_string_st *self); +LIBMEMCACHED_LOCAL +char *memcached_string_take_value(memcached_string_st *self); + LIBMEMCACHED_LOCAL char *memcached_string_value_mutable(const memcached_string_st *self); diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 1b07d9c8..589d5cb3 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -954,7 +954,8 @@ static test_return_t read_through(memcached_st *memc) &string_length, &flags, &rc); test_compare(MEMCACHED_SUCCESS, rc); - test_compare(string_length, strlen(READ_THROUGH_VALUE)); + test_compare(string_length, sizeof(READ_THROUGH_VALUE) -1); + test_true(string[sizeof(READ_THROUGH_VALUE) -1] == 0); test_strcmp(READ_THROUGH_VALUE, string); free(string); @@ -962,7 +963,9 @@ static test_return_t read_through(memcached_st *memc) &string_length, &flags, &rc); test_compare(MEMCACHED_SUCCESS, rc); - test_compare(string_length, strlen(READ_THROUGH_VALUE)); + test_true(string); + test_compare(string_length, sizeof(READ_THROUGH_VALUE) -1); + test_true(string[sizeof(READ_THROUGH_VALUE) -1] == 0); test_strcmp(READ_THROUGH_VALUE, string); free(string); @@ -2802,7 +2805,6 @@ static test_return_t user_supplied_bug15(memcached_st *memc) uint32_t x; memcached_return_t rc; const char *key= "mykey"; - char *value; size_t length; uint32_t flags; @@ -2814,13 +2816,13 @@ static test_return_t user_supplied_bug15(memcached_st *memc) test_compare(MEMCACHED_SUCCESS, rc); - value= memcached_get(memc, key, strlen(key), - &length, &flags, &rc); + char *value= memcached_get(memc, key, strlen(key), + &length, &flags, &rc); test_compare(MEMCACHED_SUCCESS, rc); - test_true(value == NULL); - test_true(length == 0); - test_true(flags == 0); + test_false(value); + test_false(length); + test_false(flags); value= memcached_get(memc, key, strlen(key), &length, &flags, &rc); -- 2.30.2