From 6504a7aa2b460cb57d4d57dcc0aa0fa3f00c2a9c Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 24 Mar 2011 17:13:54 -0700 Subject: [PATCH] Updates for strncpy. --- clients/utilities.c | 13 +++++++------ libmemcached/connect.c | 2 +- libmemcached/sasl.c | 11 ++++++----- tests/mem_functions.c | 10 +++++----- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/clients/utilities.c b/clients/utilities.c index f223ae6f..92987471 100644 --- a/clients/utilities.c +++ b/clients/utilities.c @@ -149,7 +149,7 @@ static int get_password(sasl_conn_t *conn, void *context, int id, sasl_secret_t **psecret) { (void)context; - static sasl_secret_t* x; + static sasl_secret_t* ptr; if (!conn || ! psecret || id != SASL_CB_PASS) return SASL_BADPARAM; @@ -161,14 +161,15 @@ static int get_password(sasl_conn_t *conn, void *context, int id, } size_t len= strlen(passwd); - x = realloc(x, sizeof(sasl_secret_t) + len); - if (!x) + ptr= malloc(sizeof(sasl_secret_t) + len +1); + if (! ptr) return SASL_NOMEM; - x->len = len; - strcpy((void *)x->data, passwd); + ptr->len= len; + memcpy(ptr->data, passwd, len); + ptr->data[len]= 0; - *psecret = x; + *psecret= ptr; return SASL_OK; } diff --git a/libmemcached/connect.c b/libmemcached/connect.c index 3afef7d3..58a6cfd2 100644 --- a/libmemcached/connect.c +++ b/libmemcached/connect.c @@ -335,7 +335,7 @@ static memcached_return_t unix_socket_connect(memcached_server_st *ptr) memset(&servAddr, 0, sizeof (struct sockaddr_un)); servAddr.sun_family= AF_UNIX; - strcpy(servAddr.sun_path, ptr->hostname); /* Copy filename */ + strncpy(servAddr.sun_path, ptr->hostname, sizeof(servAddr.sun_path)); /* Copy filename */ test_connect: if (connect(ptr->fd, diff --git a/libmemcached/sasl.c b/libmemcached/sasl.c index 0dedb52d..66920795 100644 --- a/libmemcached/sasl.c +++ b/libmemcached/sasl.c @@ -225,8 +225,8 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, sasl_callback_t *cb= libmemcached_calloc(ptr, 4, sizeof(sasl_callback_t)); char *name= libmemcached_malloc(ptr, strlen(username) + 1); - sasl_secret_t *secret= libmemcached_malloc(ptr, strlen(password) + 1 + sizeof(*secret)) -; + size_t password_length= strlen(password); + sasl_secret_t *secret= libmemcached_malloc(ptr, password_length +1 + sizeof(*secret)); if (cb == NULL || name == NULL || secret == NULL) { libmemcached_free(ptr, cb); @@ -236,11 +236,12 @@ memcached_return_t memcached_set_sasl_auth_data(memcached_st *ptr, } secret->len= strlen(password); - strcpy((void*)secret->data, password); + memcpy(secret->data, password, password_length); + secret->data[password_length]= 0; cb[0].id= SASL_CB_USER; cb[0].proc= get_username; - cb[0].context= strcpy(name, username); + cb[0].context= strncpy(name, username, sizeof(cb[0].context)); cb[1].id= SASL_CB_AUTHNAME; cb[1].proc= get_username; cb[1].context= name; @@ -346,7 +347,7 @@ memcached_return_t memcached_clone_sasl(memcached_st *clone, const memcached_st libmemcached_free(clone, cb); return MEMCACHED_MEMORY_ALLOCATION_FAILURE; } - strcpy(cb[x].context, source->sasl.callbacks[x].context); + strncpy(cb[x].context, source->sasl.callbacks[x].context, sizeof(cb[x].context)); } else { diff --git a/tests/mem_functions.c b/tests/mem_functions.c index 74ab33a0..fb74bfae 100644 --- a/tests/mem_functions.c +++ b/tests/mem_functions.c @@ -784,8 +784,8 @@ static memcached_return_t server_function(const memcached_st *ptr, static test_return_t memcached_server_cursor_test(memcached_st *memc) { - char context[8]; - strcpy(context, "foo bad"); + char context[10]; + strncpy(context, "foo bad", sizeof(context)); memcached_server_fn callbacks[1]; callbacks[0]= server_function; @@ -4001,13 +4001,13 @@ static test_return_t set_prefix(memcached_st *memc) /* Test a long key for failure */ /* TODO, extend test to determine based on setting, what result should be */ - strcpy(long_key, "Thisismorethentheallottednumberofcharacters"); + strncpy(long_key, "Thisismorethentheallottednumberofcharacters", sizeof(long_key)); rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key); //test_true(rc == MEMCACHED_BAD_KEY_PROVIDED); test_true(rc == MEMCACHED_SUCCESS); /* Now test a key with spaces (which will fail from long key, since bad key is not set) */ - strcpy(long_key, "This is more then the allotted number of characters"); + strncpy(long_key, "This is more then the allotted number of characters", sizeof(long_key)); rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key); test_true(rc == MEMCACHED_BAD_KEY_PROVIDED); @@ -4015,7 +4015,7 @@ static test_return_t set_prefix(memcached_st *memc) rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_VERIFY_KEY, 1); test_true(rc == MEMCACHED_SUCCESS); - strcpy(long_key, "dog cat"); + strncpy(long_key, "dog cat", sizeof(long_key)); rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key); test_true(rc == MEMCACHED_BAD_KEY_PROVIDED); } -- 2.30.2