From: Brian Aker Date: Tue, 24 May 2011 23:21:14 +0000 (-0700) Subject: Fix set_last_disconnected_host() to handle a null operation. X-Git-Tag: 0.51~8^2~1 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=4924131d4d62d724d7e590cb2c5668ac5c8a3006;p=m6w6%2Flibmemcached Fix set_last_disconnected_host() to handle a null operation. --- diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 35053d60..90d3facb 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -505,19 +505,18 @@ static memcached_return_t network_connect(memcached_server_st *ptr) return MEMCACHED_SUCCESS; /* The last error should be from connect() */ } -void set_last_disconnected_host(memcached_server_write_instance_st ptr) +void set_last_disconnected_host(memcached_server_write_instance_st self) { // const_cast - memcached_st *root= (memcached_st *)ptr->root; + memcached_st *root= (memcached_st *)self->root; #if 0 - WATCHPOINT_STRING(ptr->hostname); - WATCHPOINT_NUMBER(ptr->port); - WATCHPOINT_ERRNO(ptr->cached_errno); + WATCHPOINT_STRING(self->hostname); + WATCHPOINT_NUMBER(self->port); + WATCHPOINT_ERRNO(self->cached_errno); #endif - if (root->last_disconnected_server) - memcached_server_free(root->last_disconnected_server); - root->last_disconnected_server= memcached_server_clone(NULL, ptr); + memcached_server_free(root->last_disconnected_server); + root->last_disconnected_server= memcached_server_clone(NULL, self); } memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) @@ -570,6 +569,7 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) WATCHPOINT_ASSERT(0); rc= MEMCACHED_NOT_SUPPORTED; break; + case MEMCACHED_CONNECTION_UDP: case MEMCACHED_CONNECTION_TCP: rc= network_connect(ptr); @@ -585,15 +585,17 @@ memcached_return_t memcached_connect(memcached_server_write_instance_st ptr) } #endif break; + case MEMCACHED_CONNECTION_UNIX_SOCKET: rc= unix_socket_connect(ptr); break; + case MEMCACHED_CONNECTION_MAX: default: WATCHPOINT_ASSERT(0); } - if (rc == MEMCACHED_SUCCESS) + if (memcached_success(rc)) { ptr->server_failure_counter= 0; ptr->next_retry= 0; diff --git a/libmemcached/memcached.cc b/libmemcached/memcached.cc index 21306f2a..f2e8993b 100644 --- a/libmemcached/memcached.cc +++ b/libmemcached/memcached.cc @@ -151,8 +151,7 @@ static void _free(memcached_st *ptr, bool release_st) memcached_virtual_bucket_free(ptr); - if (ptr->last_disconnected_server) - memcached_server_free(ptr->last_disconnected_server); + memcached_server_free(ptr->last_disconnected_server); if (ptr->on_cleanup) ptr->on_cleanup(ptr); @@ -273,33 +272,27 @@ memcached_return_t memcached_reset(memcached_st *ptr) return MEMCACHED_SUCCESS; } -void memcached_servers_reset(memcached_st *ptr) +void memcached_servers_reset(memcached_st *self) { - if (not ptr) + if (not self) return; - memcached_server_list_free(memcached_server_list(ptr)); + memcached_server_list_free(memcached_server_list(self)); - memcached_server_list_set(ptr, NULL); - ptr->number_of_hosts= 0; - if (ptr->last_disconnected_server) - { - memcached_server_free(ptr->last_disconnected_server); - } - ptr->last_disconnected_server= NULL; - ptr->server_failure_limit= 0; + memcached_server_list_set(self, NULL); + self->number_of_hosts= 0; + memcached_server_free(self->last_disconnected_server); + self->last_disconnected_server= NULL; + self->server_failure_limit= 0; } -void memcached_reset_last_disconnected_server(memcached_st *ptr) +void memcached_reset_last_disconnected_server(memcached_st *self) { - if (not ptr) + if (not self) return; - if (ptr->last_disconnected_server) - { - memcached_server_free(ptr->last_disconnected_server); - ptr->last_disconnected_server= NULL; - } + memcached_server_free(self->last_disconnected_server); + self->last_disconnected_server= NULL; } void memcached_free(memcached_st *ptr) diff --git a/libmemcached/quit.cc b/libmemcached/quit.cc index cd5d9ecd..fb045ce1 100644 --- a/libmemcached/quit.cc +++ b/libmemcached/quit.cc @@ -67,7 +67,7 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) } else { - rc= memcached_do(ptr, "quit\r\n", sizeof("quit\r\n") -1, true); + rc= memcached_do(ptr, memcached_literal_param("quit\r\n"), true); } WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED); diff --git a/libmemcached/server.cc b/libmemcached/server.cc index 72f1b822..57540ccf 100644 --- a/libmemcached/server.cc +++ b/libmemcached/server.cc @@ -135,7 +135,9 @@ memcached_server_st *memcached_server_create_with(const memcached_st *memc, void memcached_server_free(memcached_server_st *self) { - assert(self); + if (not self) + return; + memcached_quit_server(self, false); if (self->cached_server_error) @@ -167,7 +169,7 @@ memcached_server_st *memcached_server_clone(memcached_server_st *destination, destination= memcached_server_create_with(source->root, destination, source->hostname, source->port, source->weight, source->type); - if (destination != NULL) + if (not destination) { destination->cached_errno= source->cached_errno; diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 712c13d9..3b417b29 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -5380,13 +5380,14 @@ static test_return_t test_get_last_disconnect(memcached_st *memc) const char *value= "milka"; memcached_reset_last_disconnected_server(memc); + test_false(memc->last_disconnected_server); rc= memcached_set(memc, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0); test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); disconnected_server = memcached_server_get_last_disconnect(memc); - test_true(disconnected_server == NULL); + test_false(disconnected_server); /* With a non existing server */ memcached_st *mine; @@ -5405,16 +5406,11 @@ static test_return_t test_get_last_disconnect(memcached_st *memc) rc= memcached_set(mine, key, strlen(key), value, strlen(value), (time_t)0, (uint32_t)0); - test_true(rc != MEMCACHED_SUCCESS); + test_true(memcached_failed(rc)); disconnected_server= memcached_server_get_last_disconnect(mine); - if (disconnected_server == NULL) - { - fprintf(stderr, "RC %s\n", memcached_strerror(mine, rc)); - abort(); - } - test_true(disconnected_server != NULL); - test_true(memcached_server_port(disconnected_server)== 9); + test_true_got(disconnected_server, memcached_strerror(mine, rc)); + test_compare(9, memcached_server_port(disconnected_server)); test_true(strncmp(memcached_server_name(disconnected_server),"localhost",9) == 0); memcached_quit(mine);