From 4b540f4666ede9b240d58b21d61af8e4179ea13c Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 21 Jan 2020 13:04:56 +0100 Subject: [PATCH] libmemcached: fix #13 reset continuum counters after freeing them --- libmemcached/memcached.cc | 2 ++ tests/libmemcached-1.0/all_tests.h | 2 ++ tests/libmemcached-1.0/server_add.cc | 14 ++++++++++++++ tests/libmemcached-1.0/setup_and_teardowns.cc | 10 ++++++++++ tests/libmemcached-1.0/setup_and_teardowns.h | 1 + tests/server_add.h | 1 + 6 files changed, 30 insertions(+) diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index 337f918d..6e88c257 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -285,6 +285,8 @@ void memcached_servers_reset(memcached_st *shell) { libmemcached_free(self, self->ketama.continuum); self->ketama.continuum= NULL; + self->ketama.continuum_count= 0; + self->ketama.continuum_points_counter= 0; memcached_instance_list_free(memcached_instance_list(self), self->number_of_hosts); memcached_instance_set(self, NULL, 0); diff --git a/tests/libmemcached-1.0/all_tests.h b/tests/libmemcached-1.0/all_tests.h index b4abe9dd..3552f3dc 100644 --- a/tests/libmemcached-1.0/all_tests.h +++ b/tests/libmemcached-1.0/all_tests.h @@ -413,6 +413,7 @@ test_st memcached_server_add_TESTS[] ={ {"memcached_server_add(NULL)", false, (test_callback_fn*)memcached_server_add_null_test }, {"memcached_server_add(many)", false, (test_callback_fn*)memcached_server_many_TEST }, {"memcached_server_add(many weighted)", false, (test_callback_fn*)memcached_server_many_weighted_TEST }, + {"memcached_servers_reset(\"\")", false, (test_callback_fn*)memcached_servers_reset_test}, {0, 0, (test_callback_fn*)0} }; @@ -452,6 +453,7 @@ collection_st collection[] ={ {"hsieh_availability", 0, 0, hsieh_availability}, {"murmur_availability", 0, 0, murmur_availability}, {"memcached_server_add", (test_callback_fn*)memcached_servers_reset_SETUP, 0, memcached_server_add_TESTS}, + {"memcached_server_add(continuum)", (test_callback_fn*)memcached_servers_reset_CONTINUUM, 0, memcached_server_add_TESTS}, {"memcached_server_add(MEMCACHED_DISTRIBUTION_CONSISTENT)", (test_callback_fn*)memcached_servers_reset_MEMCACHED_DISTRIBUTION_CONSISTENT_SETUP, 0, memcached_server_add_TESTS}, {"memcached_server_add(MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED)", (test_callback_fn*)memcached_servers_reset_MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED_SETUP, 0, memcached_server_add_TESTS}, {"block", 0, 0, tests}, diff --git a/tests/libmemcached-1.0/server_add.cc b/tests/libmemcached-1.0/server_add.cc index 37470947..49eb8448 100644 --- a/tests/libmemcached-1.0/server_add.cc +++ b/tests/libmemcached-1.0/server_add.cc @@ -102,3 +102,17 @@ test_return_t memcached_server_many_weighted_TEST(memcached_st* memc) return TEST_SUCCESS; } + +test_return_t memcached_servers_reset_test(memcached_st* memc) +{ + ASSERT_EQ(0, memcached_server_count(memc)); + + test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "localhost", 11511)); + + memcached_servers_reset(memc); + + test_compare(MEMCACHED_SUCCESS, memcached_server_add(memc, "localhost", 11511)); + + return TEST_SUCCESS; +} + diff --git a/tests/libmemcached-1.0/setup_and_teardowns.cc b/tests/libmemcached-1.0/setup_and_teardowns.cc index 4191c384..047497d6 100644 --- a/tests/libmemcached-1.0/setup_and_teardowns.cc +++ b/tests/libmemcached-1.0/setup_and_teardowns.cc @@ -177,6 +177,16 @@ test_return_t memcached_servers_reset_SETUP(memcached_st *memc) return TEST_SUCCESS; } +test_return_t memcached_servers_reset_CONTINUUM(memcached_st *memc) +{ + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_DISTRIBUTION, MEMCACHED_DISTRIBUTION_CONSISTENT); + memcached_servers_reset(memc); + test_zero(memc->ketama.continuum_count); + // If memc->ketama.continuum_count is non-zero at this point, any call to + // memcached_server_add will cause a segfault. + return TEST_SUCCESS; +} + test_return_t memcached_servers_reset_MEMCACHED_DISTRIBUTION_CONSISTENT_SETUP(memcached_st *memc) { test_compare(TEST_SUCCESS, memcached_servers_reset_SETUP(memc)); diff --git a/tests/libmemcached-1.0/setup_and_teardowns.h b/tests/libmemcached-1.0/setup_and_teardowns.h index 7610c5a6..59b56f67 100644 --- a/tests/libmemcached-1.0/setup_and_teardowns.h +++ b/tests/libmemcached-1.0/setup_and_teardowns.h @@ -64,3 +64,4 @@ test_return_t pre_buffer(memcached_st*); test_return_t memcached_servers_reset_MEMCACHED_DISTRIBUTION_CONSISTENT_SETUP(memcached_st *memc); test_return_t memcached_servers_reset_MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED_SETUP(memcached_st *memc); test_return_t memcached_servers_reset_SETUP(memcached_st *memc); +test_return_t memcached_servers_reset_CONTINUUM(memcached_st *memc); diff --git a/tests/server_add.h b/tests/server_add.h index 06d58356..426571c4 100644 --- a/tests/server_add.h +++ b/tests/server_add.h @@ -41,3 +41,4 @@ test_return_t memcached_server_add_null_test(memcached_st*); test_return_t memcached_server_add_empty_test(memcached_st*); test_return_t memcached_server_many_TEST(memcached_st*); test_return_t memcached_server_many_weighted_TEST(memcached_st*); +test_return_t memcached_servers_reset_test(memcached_st*); -- 2.30.2