From f67f83b1facdb6515fb0996fd70d172df37d5962 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 27 Oct 2004 06:03:28 +0000 Subject: [PATCH] Use strlcpy, not strncpy svn:r2603 --- src/common/crypto.c | 3 +-- src/common/util.c | 6 +++--- src/or/dirserv.c | 4 +--- src/or/dns.c | 15 +++++---------- 4 files changed, 10 insertions(+), 18 deletions(-) diff --git a/src/common/crypto.c b/src/common/crypto.c index f896b27a58..0ab6d3d3c4 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -462,8 +462,7 @@ crypto_pk_write_private_key_to_filename(crypto_pk_env_t *env, len = BIO_get_mem_data(bio, &cp); tor_assert(len >= 0); s = tor_malloc(len+1); - strncpy(s, cp, len); - s[len] = '\0'; + strlcpy(s, cp, len+1); r = write_str_to_file(fname, s, 0); BIO_free(bio); free(s); diff --git a/src/common/util.c b/src/common/util.c index f94088ec9a..4084fe1bb8 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -195,8 +195,7 @@ char *tor_strndup(const char *s, size_t n) { char *dup; tor_assert(s); dup = tor_malloc(n+1); - strncpy(dup, s, n); - dup[n] = 0; + strlcpy(dup, s, n+1); return dup; } @@ -1770,7 +1769,8 @@ char *expand_filename(const char *filename) log_fn(LOG_WARN, "Couldn't find $HOME environment variable while expanding %s", filename); return NULL; } - /* minus two characters for ~/, plus one for /, plus one for NUL. */ + /* minus two characters for ~/, plus one for /, plus one for NUL. + * Round up to 16 in case we can't do math. */ len = strlen(home)+strlen(filename)+16; result = tor_malloc(len); snprintf(result,len,"%s/%s",home,filename+2); diff --git a/src/or/dirserv.c b/src/or/dirserv.c index d78ab7c45c..f108178464 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -386,9 +386,7 @@ dirserv_add_descriptor(const char **desc) ent->nickname = tor_strdup(ri->nickname); ent->published = ri->published_on; ent->desc_len = desc_len; - ent->descriptor = tor_malloc(desc_len+1); - strncpy(ent->descriptor, start, desc_len); - ent->descriptor[desc_len] = '\0'; + ent->descriptor = tor_strndup(start,desc_len); ent->router = ri; /* XXX008 is ent->verified useful/used for anything? */ ent->verified = verified; /* XXXX008 support other possibilities. */ diff --git a/src/or/dns.c b/src/or/dns.c index 17bb58c594..b097c1d24a 100644 --- a/src/or/dns.c +++ b/src/or/dns.c @@ -201,8 +201,7 @@ int dns_resolve(connection_t *exitconn) { purge_expired_resolves(now); /* now check the tree to see if 'address' is already there. */ - strncpy(search.address, exitconn->address, MAX_ADDRESSLEN); - search.address[MAX_ADDRESSLEN-1] = 0; + strlcpy(search.address, exitconn->address, sizeof(search.address)); resolve = SPLAY_FIND(cache_tree, &cache_root, &search); if(resolve) { /* already there */ switch(resolve->state) { @@ -237,8 +236,7 @@ int dns_resolve(connection_t *exitconn) { resolve = tor_malloc_zero(sizeof(struct cached_resolve)); resolve->state = CACHE_STATE_PENDING; resolve->expire = now + MAX_DNS_ENTRY_AGE; - strncpy(resolve->address, exitconn->address, MAX_ADDRESSLEN); - resolve->address[MAX_ADDRESSLEN-1] = 0; + strlcpy(resolve->address, exitconn->address, sizeof(resolve->address)); /* add us to the pending list */ pending_connection = tor_malloc_zero(sizeof(struct pending_connection_t)); @@ -306,8 +304,7 @@ void connection_dns_remove(connection_t *conn) tor_assert(conn->type == CONN_TYPE_EXIT); tor_assert(conn->state == EXIT_CONN_STATE_RESOLVING); - strncpy(search.address, conn->address, MAX_ADDRESSLEN); - search.address[MAX_ADDRESSLEN-1] = 0; + strlcpy(search.address, conn->address, sizeof(search.address)); resolve = SPLAY_FIND(cache_tree, &cache_root, &search); if(!resolve) { @@ -383,8 +380,7 @@ void dns_cancel_pending_resolve(char *address) { struct cached_resolve *resolve; connection_t *pendconn; - strncpy(search.address, address, MAX_ADDRESSLEN); - search.address[MAX_ADDRESSLEN-1] = 0; + strlcpy(search.address, address, sizeof(search.address)); resolve = SPLAY_FIND(cache_tree, &cache_root, &search); if(!resolve) { @@ -454,8 +450,7 @@ static void dns_found_answer(char *address, uint32_t addr, char outcome) { connection_t *pendconn; circuit_t *circ; - strncpy(search.address, address, MAX_ADDRESSLEN); - search.address[MAX_ADDRESSLEN-1] = 0; + strlcpy(search.address, address, sizeof(search.address)); resolve = SPLAY_FIND(cache_tree, &cache_root, &search); if(!resolve) {