From 48297ab2ce05feb4a81e8322940bf102e505d2d4 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Fri, 11 Feb 2011 09:57:27 -0800 Subject: [PATCH] Remove openbsd warnings. --- clients/memcapable.c | 40 ++++++++--------- clients/memslap.c | 101 ++++++++++++++++++++++++++---------------- clients/ms_conn.c | 13 +++--- libmemcached/sasl.c | 8 ++-- tests/mem_functions.c | 4 +- tests/server.c | 11 ++++- 6 files changed, 103 insertions(+), 74 deletions(-) diff --git a/clients/memcapable.c b/clients/memcapable.c index a5a8185a..3de6baf1 100644 --- a/clients/memcapable.c +++ b/clients/memcapable.c @@ -1267,8 +1267,7 @@ static enum test_return test_ascii_set_impl(const char* key, bool noreply) { /* @todo add tests for bogus format! */ char buffer[1024]; - sprintf(buffer, "set %s 0 0 5%s\r\nvalue\r\n", key, - noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "set %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); if (!noreply) @@ -1291,8 +1290,7 @@ static enum test_return test_ascii_add_impl(const char* key, bool noreply) { /* @todo add tests for bogus format! */ char buffer[1024]; - sprintf(buffer, "add %s 0 0 5%s\r\nvalue\r\n", key, - noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "add %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); if (!noreply) @@ -1394,7 +1392,7 @@ static enum test_return ascii_get_item(const char *key, const char *value, datasize= strlen(value); verify(datasize < sizeof(buffer)); - sprintf(buffer, "get %s\r\n", key); + snprintf(buffer, sizeof(buffer), "get %s\r\n", key); execute(send_string(buffer)); if (exist) @@ -1455,7 +1453,7 @@ static enum test_return ascii_gets_item(const char *key, const char *value, datasize= strlen(value); verify(datasize < sizeof(buffer)); - sprintf(buffer, "gets %s\r\n", key); + snprintf(buffer, sizeof(buffer), "gets %s\r\n", key); execute(send_string(buffer)); if (exist) @@ -1471,7 +1469,7 @@ static enum test_return ascii_set_item(const char *key, const char *value) { char buffer[300]; size_t len= strlen(value); - sprintf(buffer, "set %s 0 0 %u\r\n", key, (unsigned int)len); + snprintf(buffer, sizeof(buffer), "set %s 0 0 %u\r\n", key, (unsigned int)len); execute(send_string(buffer)); execute(retry_write(value, len)); execute(send_string("\r\n")); @@ -1482,8 +1480,7 @@ static enum test_return ascii_set_item(const char *key, const char *value) static enum test_return test_ascii_replace_impl(const char* key, bool noreply) { char buffer[1024]; - sprintf(buffer, "replace %s 0 0 5%s\r\nvalue\r\n", key, - noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "replace %s 0 0 5%s\r\nvalue\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); if (noreply) @@ -1523,8 +1520,7 @@ static enum test_return test_ascii_cas_impl(const char* key, bool noreply) execute(ascii_set_item(key, "value")); execute(ascii_gets_item(key, "value", true, &cas)); - sprintf(buffer, "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, - noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "cas %s 0 0 6 %lu%s\r\nvalue2\r\n", key, cas, noreply ? " noreply" : ""); execute(send_string(buffer)); if (noreply) @@ -1564,7 +1560,7 @@ static enum test_return test_ascii_delete_impl(const char *key, bool noreply) execute(receive_error_response()); char buffer[1024]; - sprintf(buffer, "delete %s%s\r\n", key, noreply ? " noreply" : ""); + snprintf(buffer, sizeof(buffer), "delete %s%s\r\n", key, noreply ? " noreply" : ""); execute(send_string(buffer)); if (noreply) @@ -1677,7 +1673,7 @@ static enum test_return test_ascii_mget(void) static enum test_return test_ascii_incr_impl(const char* key, bool noreply) { char cmd[300]; - sprintf(cmd, "incr %s 1%s\r\n", key, noreply ? " noreply" : ""); + snprintf(cmd, sizeof(cmd), "incr %s 1%s\r\n", key, noreply ? " noreply" : ""); execute(ascii_set_item(key, "0")); for (int x= 1; x < 11; ++x) @@ -1713,7 +1709,7 @@ static enum test_return test_ascii_incr_noreply(void) static enum test_return test_ascii_decr_impl(const char* key, bool noreply) { char cmd[300]; - sprintf(cmd, "decr %s 1%s\r\n", key, noreply ? " noreply" : ""); + snprintf(cmd, sizeof(cmd), "decr %s 1%s\r\n", key, noreply ? " noreply" : ""); execute(ascii_set_item(key, "9")); for (int x= 8; x > -1; --x) @@ -1815,10 +1811,10 @@ static enum test_return test_ascii_concat_impl(const char *key, value="hello"; char cmd[400]; - sprintf(cmd, "%s %s 0 0 %u%s\r\n%s\r\n", - append ? "append" : "prepend", - key, (unsigned int)strlen(value), noreply ? " noreply" : "", - value); + snprintf(cmd, sizeof(cmd), "%s %s 0 0 %u%s\r\n%s\r\n", + append ? "append" : "prepend", + key, (unsigned int)strlen(value), noreply ? " noreply" : "", + value); execute(send_string(cmd)); if (noreply) @@ -1828,10 +1824,10 @@ static enum test_return test_ascii_concat_impl(const char *key, execute(ascii_get_item(key, "hello world", true)); - sprintf(cmd, "%s %s_notfound 0 0 %u%s\r\n%s\r\n", - append ? "append" : "prepend", - key, (unsigned int)strlen(value), noreply ? " noreply" : "", - value); + snprintf(cmd, sizeof(cmd), "%s %s_notfound 0 0 %u%s\r\n%s\r\n", + append ? "append" : "prepend", + key, (unsigned int)strlen(value), noreply ? " noreply" : "", + value); execute(send_string(cmd)); if (noreply) diff --git a/clients/memslap.c b/clients/memslap.c index 38343b86..94048802 100644 --- a/clients/memslap.c +++ b/clients/memslap.c @@ -706,45 +706,69 @@ static void ms_print_memslap_stats(struct timeval *start_time, char buf[1024]; char *pos= buf; - pos+= sprintf(pos, "cmd_get: %zu\n", - ms_stats.cmd_get); - pos+= sprintf(pos, "cmd_set: %zu\n", - ms_stats.cmd_set); - pos+= sprintf(pos, "get_misses: %zu\n", - ms_stats.get_misses); + pos+= snprintf(pos, + sizeof(buf), "cmd_get: %lu\n", + (unsigned long) ms_stats.cmd_get); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "cmd_set: %lu\n", + (unsigned long) ms_stats.cmd_set); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "get_misses: %lu\n", + (unsigned long) ms_stats.get_misses); if (ms_setting.verify_percent > 0) { - pos+= sprintf(pos, "verify_misses: %zu\n", - ms_stats.vef_miss); - pos+= sprintf(pos, "verify_failed: %zu\n", - ms_stats.vef_failed); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "verify_misses: %lu\n", + (unsigned long) ms_stats.vef_miss); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "verify_failed: %lu\n", + (unsigned long) ms_stats.vef_failed); } if (ms_setting.exp_ver_per > 0) { - pos+= sprintf(pos, "expired_get: %zu\n", - ms_stats.exp_get); - pos+= sprintf(pos, "unexpired_unget: %zu\n", - ms_stats.unexp_unget); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "expired_get: %lu\n", + (unsigned long) ms_stats.exp_get); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "unexpired_unget: %lu\n", + (unsigned long) ms_stats.unexp_unget); } - pos+= sprintf(pos, - "written_bytes: %zu\n", - ms_stats.bytes_written); - pos+= sprintf(pos, "read_bytes: %zu\n", - ms_stats.bytes_read); - pos+= sprintf(pos, "object_bytes: %zu\n", - ms_stats.obj_bytes); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "written_bytes: %lu\n", + (unsigned long) ms_stats.bytes_written); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "read_bytes: %lu\n", + (unsigned long) ms_stats.bytes_read); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "object_bytes: %lu\n", + (unsigned long) ms_stats.obj_bytes); if (ms_setting.udp || ms_setting.facebook_test) { - pos+= sprintf(pos, "packet_disorder: %zu\n", - ms_stats.pkt_disorder); - pos+= sprintf(pos, "packet_drop: %zu\n", - ms_stats.pkt_drop); - pos+= sprintf(pos, "udp_timeout: %zu\n", - ms_stats.udp_timeout); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "packet_disorder: %lu\n", + (unsigned long) ms_stats.pkt_disorder); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "packet_drop: %lu\n", + (unsigned long)ms_stats.pkt_drop); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "udp_timeout: %lu\n", + (unsigned long)ms_stats.udp_timeout); } if (ms_setting.stat_freq > 0) @@ -755,17 +779,18 @@ static void ms_print_memslap_stats(struct timeval *start_time, } int64_t time_diff= ms_time_diff(start_time, end_time); - pos+= sprintf( - pos, - "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n", - (double)time_diff / 1000000, - (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set), - (ms_stats.cmd_get - + ms_stats.cmd_set) / ((long double)time_diff / 1000000), - (double)( - ms_stats.bytes_written - + ms_stats.bytes_read) / 1024 / 1024 - / ((double)time_diff / 1000000)); + pos+= snprintf(pos, + sizeof(buf) - (size_t)(pos -buf), + "\nRun time: %.1fs Ops: %llu TPS: %.0Lf Net_rate: %.1fM/s\n", + (double)time_diff / 1000000, + (unsigned long long)(ms_stats.cmd_get + ms_stats.cmd_set), + (ms_stats.cmd_get + + ms_stats.cmd_set) / ((long double)time_diff / 1000000), + (double)( + ms_stats.bytes_written + + ms_stats.bytes_read) / 1024 / 1024 + / ((double)time_diff / 1000000)); + assert(pos <= buf); fprintf(stdout, "%s", buf); fflush(stdout); diff --git a/clients/ms_conn.c b/clients/ms_conn.c index 82016e1f..d7cd2de0 100644 --- a/clients/ms_conn.c +++ b/clients/ms_conn.c @@ -2886,13 +2886,14 @@ static int ms_build_ascii_write_buf_set(ms_conn_t *c, ms_task_item_t *item) int write_len; char *buffer= c->wbuf; - write_len= sprintf(buffer, - " %u %d %d\r\n", - 0, - item->exp_time, - item->value_size); + write_len= snprintf(buffer, + c->wsize, + " %u %d %d\r\n", + 0, + item->exp_time, + item->value_size); - if (write_len > c->wsize) + if (write_len > c->wsize || write_len < 0) { /* ought to be always enough. just fail for simplicity */ fprintf(stderr, "output command line too long.\n"); diff --git a/libmemcached/sasl.c b/libmemcached/sasl.c index ef52c7c3..0dedb52d 100644 --- a/libmemcached/sasl.c +++ b/libmemcached/sasl.c @@ -30,7 +30,7 @@ const sasl_callback_t *memcached_get_sasl_callbacks(memcached_st *ptr) * @param raddr remote address (out) * @return true on success false otherwise (errno contains more info) */ -static bool resolve_names(int fd, char *laddr, char *raddr) +static bool resolve_names(int fd, char *laddr, size_t laddr_length, char *raddr, size_t raddr_length) { char host[NI_MAXHOST]; char port[NI_MAXSERV]; @@ -44,7 +44,7 @@ static bool resolve_names(int fd, char *laddr, char *raddr) return false; } - (void)sprintf(laddr, "%s;%s", host, port); + (void)snprintf(laddr, laddr_length, "%s;%s", host, port); salen= sizeof(saddr); if ((getpeername(fd, (struct sockaddr *)&saddr, &salen) < 0) || @@ -54,7 +54,7 @@ static bool resolve_names(int fd, char *laddr, char *raddr) return false; } - (void)sprintf(raddr, "%s;%s", host, port); + (void)snprintf(raddr, raddr_length, "%s;%s", host, port); return true; } @@ -108,7 +108,7 @@ memcached_return_t memcached_sasl_authenticate_connection(memcached_server_st *s char laddr[NI_MAXHOST + NI_MAXSERV]; char raddr[NI_MAXHOST + NI_MAXSERV]; - unlikely (!resolve_names(server->fd, laddr, raddr)) + unlikely (!resolve_names(server->fd, laddr, sizeof(laddr), raddr, sizeof(raddr))) { server->cached_errno= errno; return MEMCACHED_ERRNO; diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 740e34de..3524999a 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -1031,7 +1031,7 @@ static test_return_t set_test3(memcached_st *memc) { char key[16]; - sprintf(key, "foo%u", x); + snprintf(key, sizeof(key), "foo%u", x); rc= memcached_set(memc, key, strlen(key), value, value_length, @@ -3162,7 +3162,7 @@ static test_return_t output_ketama_weighted_keys(memcached_st *trash) for (int x= 0; x < 10000; x++) { char key[10]; - sprintf(key, "%d", x); + snprintf(key, sizeof(key), "%d", x); uint32_t server_idx = memcached_generate_hash(memc, key, strlen(key)); char *hostname = memc->hosts[server_idx].hostname; diff --git a/tests/server.c b/tests/server.c index 4db656f1..84f15de0 100644 --- a/tests/server.c +++ b/tests/server.c @@ -97,7 +97,6 @@ void server_startup(server_startup_st *construct) for (uint32_t x= 0; x < construct->count; x++) { - int count; int status; in_port_t port; @@ -140,7 +139,15 @@ void server_startup(server_startup_st *construct) status= system(buffer); fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status); } - count= sprintf(end_ptr, "localhost:%u,", port); + int count; + size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer); + count= snprintf(end_ptr, remaining_length, "localhost:%u,", port); + + if ((size_t)count >= remaining_length || count < 0) + { + fprintf(stderr, "server names grew to be larger then buffer allowed\n"); + abort(); + } end_ptr+= count; } *end_ptr= 0; -- 2.30.2