[or-cvs] [tor/master] Make tor_addr_copy() conform to memcpy requirements

nickm at torproject.org nickm at torproject.org
Tue Feb 9 17:33:01 UTC 2010


Author: Nick Mathewson <nickm at torproject.org>
Date: Tue, 9 Feb 2010 12:32:10 -0500
Subject: Make tor_addr_copy() conform to memcpy requirements
Commit: c0d682686ad8df66b1f6680b1185853532f24429

The src and dest of a memcpy() call aren't supposed to overlap,
but we were sometimes calling tor_addr_copy() as a no-op.

Also, tor_addr_assign was a redundant copy of tor_addr_copy(); this patch
removes it.
---
 src/common/address.c     |    9 ++-------
 src/common/address.h     |    1 -
 src/or/connection_edge.c |    4 ++--
 src/or/dns.c             |    2 +-
 4 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/common/address.c b/src/common/address.c
index 2fe013a..7729f29 100644
--- a/src/common/address.c
+++ b/src/common/address.c
@@ -760,6 +760,8 @@ tor_addr_from_in6(tor_addr_t *dest, const struct in6_addr *in6)
 void
 tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src)
 {
+  if (src == dest)
+    return;
   tor_assert(src);
   tor_assert(dest);
   memcpy(dest, src, sizeof(tor_addr_t));
@@ -912,13 +914,6 @@ tor_dup_addr(const tor_addr_t *addr)
   return tor_strdup(buf);
 }
 
-/** Copy the address in <b>src</b> to <b>dest</b> */
-void
-tor_addr_assign(tor_addr_t *dest, const tor_addr_t *src)
-{
-  memcpy(dest, src, sizeof(tor_addr_t));
-}
-
 /** Return a string representing the address <b>addr</b>.  This string is
  * statically allocated, and must not be freed.  Each call to
  * <b>fmt_addr</b> invalidates the last result of the function.  This
diff --git a/src/common/address.h b/src/common/address.h
index 9480d64..4d4c910 100644
--- a/src/common/address.h
+++ b/src/common/address.h
@@ -107,7 +107,6 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
 
 int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
 char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
-void tor_addr_assign(tor_addr_t *dest, const tor_addr_t *src);
 const char *fmt_addr(const tor_addr_t *addr);
 int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);
 
diff --git a/src/or/connection_edge.c b/src/or/connection_edge.c
index 8447853..8bb6742 100644
--- a/src/or/connection_edge.c
+++ b/src/or/connection_edge.c
@@ -2607,7 +2607,7 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
   if (rh.command == RELAY_COMMAND_BEGIN_DIR) {
     tor_assert(or_circ);
     if (or_circ->p_conn && !tor_addr_is_null(&or_circ->p_conn->real_addr))
-      tor_addr_assign(&n_stream->_base.addr, &or_circ->p_conn->real_addr);
+      tor_addr_copy(&n_stream->_base.addr, &or_circ->p_conn->real_addr);
     return connection_exit_connect_dir(n_stream);
   }
 
@@ -2779,7 +2779,7 @@ connection_exit_connect_dir(edge_connection_t *exitconn)
 
   dirconn = dir_connection_new(AF_INET);
 
-  tor_addr_assign(&dirconn->_base.addr, &exitconn->_base.addr);
+  tor_addr_copy(&dirconn->_base.addr, &exitconn->_base.addr);
   dirconn->_base.port = 0;
   dirconn->_base.address = tor_strdup(exitconn->_base.address);
   dirconn->_base.type = CONN_TYPE_DIR;
diff --git a/src/or/dns.c b/src/or/dns.c
index 08bff76..c055c88 100644
--- a/src/or/dns.c
+++ b/src/or/dns.c
@@ -655,7 +655,7 @@ dns_resolve_impl(edge_connection_t *exitconn, int is_resolve,
    * know the answer. */
   if (tor_addr_from_str(&addr, exitconn->_base.address) >= 0) {
     if (tor_addr_family(&addr) == AF_INET) {
-      tor_addr_assign(&exitconn->_base.addr, &addr);
+      tor_addr_copy(&exitconn->_base.addr, &addr);
       exitconn->address_ttl = DEFAULT_DNS_TTL;
       return 1;
     } else {
-- 
1.6.5



More information about the tor-commits mailing list