From: Brian Aker Date: Mon, 7 Apr 2008 18:22:00 +0000 (-0700) Subject: Fix for sorting when no servers have been added. X-Git-Tag: 0.20~1^2~24 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=2cf40dcff3bf15d6ca5c2f1c23b9269bc80419b1;p=awesomized%2Flibmemcached Fix for sorting when no servers have been added. --- diff --git a/libmemcached/memcached_hosts.c b/libmemcached/memcached_hosts.c index 863e7674..76fd5a01 100644 --- a/libmemcached/memcached_hosts.c +++ b/libmemcached/memcached_hosts.c @@ -51,8 +51,11 @@ static int compare_servers(const void *p1, const void *p2) void sort_hosts(memcached_st *ptr) { - qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); - ptr->hosts[0].count= ptr->number_of_hosts; + if (ptr->number_of_hosts) + { + qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers); + ptr->hosts[0].count= ptr->number_of_hosts; + } } static void host_reset(memcached_st *ptr, memcached_server_st *host, @@ -122,7 +125,7 @@ memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *l } ptr->hosts[0].count= ptr->number_of_hosts; - if (ptr->number_of_hosts > 1 && ptr->flags & MEM_USE_SORT_HOSTS) + if (ptr->flags & MEM_USE_SORT_HOSTS) sort_hosts(ptr); rebalance_wheel(ptr); @@ -186,7 +189,7 @@ static memcached_return server_add(memcached_st *ptr, char *hostname, host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type); ptr->number_of_hosts++; - if (ptr->number_of_hosts > 1 && ptr->flags & MEM_USE_SORT_HOSTS) + if (ptr->flags & MEM_USE_SORT_HOSTS) sort_hosts(ptr); ptr->hosts[0].count= ptr->number_of_hosts;