From 51e7b091691cb268aead6598c52bebaca31c5777 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 24 Mar 2011 17:29:31 -0700 Subject: [PATCH] Fix strlen vs sizeof usage in a few spots. --- libmemcached/quit.c | 13 ++++--------- libmemcached/stats.c | 44 +++++++++++++++++++++--------------------- libmemcached/version.c | 2 +- tests/mem_functions.c | 2 ++ 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/libmemcached/quit.c b/libmemcached/quit.c index 251f578c..a8247000 100644 --- a/libmemcached/quit.c +++ b/libmemcached/quit.c @@ -11,7 +11,7 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) { - if (ptr->fd != -1) + if (ptr->fd != INVALID_SOCKET) { if (io_death == false && ptr->type != MEMCACHED_CONNECTION_UDP && ptr->options.is_shutting_down == false) { @@ -30,7 +30,7 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) } else { - rc= memcached_do(ptr, "quit\r\n", strlen("quit\r\n"), true); + rc= memcached_do(ptr, "quit\r\n", sizeof("quit\r\n") -1, true); } WATCHPOINT_ASSERT(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_FETCH_NOTFINISHED); @@ -64,7 +64,7 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) memcached_io_close(ptr); } - ptr->fd= -1; + ptr->fd= INVALID_SOCKET; ptr->io_bytes_sent= 0; ptr->write_buffer_offset= (size_t) ((ptr->type == MEMCACHED_CONNECTION_UDP) ? UDP_DATAGRAM_HEADER_LENGTH : 0); ptr->read_buffer_length= 0; @@ -85,14 +85,9 @@ void memcached_quit_server(memcached_server_st *ptr, bool io_death) void memcached_quit(memcached_st *ptr) { - uint32_t x; - - if (memcached_server_count(ptr) == 0) - return; - if (memcached_server_count(ptr)) { - for (x= 0; x < memcached_server_count(ptr); x++) + for (uint32_t x= 0; x < memcached_server_count(ptr); x++) { memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x); diff --git a/libmemcached/stats.c b/libmemcached/stats.c index ebbc63ab..5a37dfd9 100644 --- a/libmemcached/stats.c +++ b/libmemcached/stats.c @@ -178,49 +178,49 @@ char *memcached_stat_get_value(const memcached_st *ptr, memcached_stat_st *memc_ *error= MEMCACHED_SUCCESS; - if (!memcmp("pid", key, strlen("pid"))) + if (!memcmp("pid", key, sizeof("pid") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pid); - else if (!memcmp("uptime", key, strlen("uptime"))) + else if (!memcmp("uptime", key, sizeof("uptime") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->uptime); - else if (!memcmp("time", key, strlen("time"))) + else if (!memcmp("time", key, sizeof("time") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->time); - else if (!memcmp("version", key, strlen("version"))) + else if (!memcmp("version", key, sizeof("version") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%s", memc_stat->version); - else if (!memcmp("pointer_size", key, strlen("pointer_size"))) + else if (!memcmp("pointer_size", key, sizeof("pointer_size") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->pointer_size); - else if (!memcmp("rusage_user", key, strlen("rusage_user"))) + else if (!memcmp("rusage_user", key, sizeof("rusage_user") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_user_seconds, memc_stat->rusage_user_microseconds); - else if (!memcmp("rusage_system", key, strlen("rusage_system"))) + else if (!memcmp("rusage_system", key, sizeof("rusage_system") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", memc_stat->rusage_system_seconds, memc_stat->rusage_system_microseconds); - else if (!memcmp("curr_items", key, strlen("curr_items"))) + else if (!memcmp("curr_items", key, sizeof("curr_items") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_items); - else if (!memcmp("total_items", key, strlen("total_items"))) + else if (!memcmp("total_items", key, sizeof("total_items") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_items); - else if (!memcmp("curr_connections", key, strlen("curr_connections"))) + else if (!memcmp("curr_connections", key, sizeof("curr_connections") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->curr_connections); - else if (!memcmp("total_connections", key, strlen("total_connections"))) + else if (!memcmp("total_connections", key, sizeof("total_connections") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->total_connections); - else if (!memcmp("connection_structures", key, strlen("connection_structures"))) + else if (!memcmp("connection_structures", key, sizeof("connection_structures") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->connection_structures); - else if (!memcmp("cmd_get", key, strlen("cmd_get"))) + else if (!memcmp("cmd_get", key, sizeof("cmd_get") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_get); - else if (!memcmp("cmd_set", key, strlen("cmd_set"))) + else if (!memcmp("cmd_set", key, sizeof("cmd_set") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->cmd_set); - else if (!memcmp("get_hits", key, strlen("get_hits"))) + else if (!memcmp("get_hits", key, sizeof("get_hits") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_hits); - else if (!memcmp("get_misses", key, strlen("get_misses"))) + else if (!memcmp("get_misses", key, sizeof("get_misses") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->get_misses); - else if (!memcmp("evictions", key, strlen("evictions"))) + else if (!memcmp("evictions", key, sizeof("evictions") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->evictions); - else if (!memcmp("bytes_read", key, strlen("bytes_read"))) + else if (!memcmp("bytes_read", key, sizeof("bytes_read") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_read); - else if (!memcmp("bytes_written", key, strlen("bytes_written"))) + else if (!memcmp("bytes_written", key, sizeof("bytes_written") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes_written); - else if (!memcmp("bytes", key, strlen("bytes"))) + else if (!memcmp("bytes", key, sizeof("bytes") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->bytes); - else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes"))) + else if (!memcmp("limit_maxbytes", key, sizeof("limit_maxbytes") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)memc_stat->limit_maxbytes); - else if (!memcmp("threads", key, strlen("threads"))) + else if (! memcmp("threads", key, sizeof("threads") -1)) length= snprintf(buffer, SMALL_STRING_LEN,"%u", memc_stat->threads); else { diff --git a/libmemcached/version.c b/libmemcached/version.c index cb8fa0e0..82de87d3 100644 --- a/libmemcached/version.c +++ b/libmemcached/version.c @@ -31,7 +31,7 @@ static inline memcached_return_t memcached_version_textual(memcached_st *ptr) char *response_ptr; const char *command= "version\r\n"; - send_length= strlen(command); + send_length= sizeof("version\r\n") -1; rc= MEMCACHED_SUCCESS; for (uint32_t x= 0; x < memcached_server_count(ptr); x++) diff --git a/tests/mem_functions.c b/tests/mem_functions.c index fb74bfae..c46ef51a 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -5967,9 +5967,11 @@ static test_return_t wrong_failure_counter_two_test(memcached_st *memc) /* put failure limit to 1 */ rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_SERVER_FAILURE_LIMIT, 1); assert(rc == MEMCACHED_SUCCESS); + /* Put a retry timeout to effectively activate failure_limit effect */ rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_RETRY_TIMEOUT, 1); assert(rc == MEMCACHED_SUCCESS); + /* change behavior that triggers memcached_quit()*/ rc= memcached_behavior_set(memc_clone, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1); assert(rc == MEMCACHED_SUCCESS); -- 2.30.2