[tor-commits] [tor/master] Define and use TOR_ADDRPORT_BUF_LEN

nickm at torproject.org nickm at torproject.org
Tue Jun 9 19:45:23 UTC 2020


commit 0daa1da3ba0ba1dbf2892e042573887f430b32fd
Author: Neel Chauhan <neel at neelc.org>
Date:   Sun Apr 26 13:06:11 2020 -0700

    Define and use TOR_ADDRPORT_BUF_LEN
---
 changes/ticket33956      |  5 +++++
 src/core/or/channeltls.c | 10 +++-------
 src/lib/net/address.c    |  3 +--
 src/lib/net/address.h    |  9 +++++++++
 4 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/changes/ticket33956 b/changes/ticket33956
new file mode 100644
index 000000000..7ad802797
--- /dev/null
+++ b/changes/ticket33956
@@ -0,0 +1,5 @@
+  o Code simplification and refactoring:
+    - Define and use a new constant TOR_ADDRPORT_BUF_LEN which is like
+      TOR_ADDR_BUF_LEN but includes enough space for an IP address,
+      brackets, seperating colon, and port number. Closes ticket 33956.
+      Patch by Neel Chauhan.
diff --git a/src/core/or/channeltls.c b/src/core/or/channeltls.c
index be941c176..484727268 100644
--- a/src/core/or/channeltls.c
+++ b/src/core/or/channeltls.c
@@ -564,10 +564,7 @@ channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
 static const char *
 channel_tls_get_remote_descr_method(channel_t *chan, int flags)
 {
-  /* IPv6 address, colon, port */
-#define MAX_DESCR_LEN (TOR_ADDR_BUF_LEN + 1 + 5)
-
-  static char buf[MAX_DESCR_LEN + 1];
+  static char buf[TOR_ADDRPORT_BUF_LEN];
   channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
   connection_t *conn;
   const char *answer = NULL;
@@ -580,15 +577,14 @@ channel_tls_get_remote_descr_method(channel_t *chan, int flags)
     switch (flags) {
       case 0:
         /* Canonical address with port*/
-        tor_snprintf(buf, MAX_DESCR_LEN + 1,
+        tor_snprintf(buf, TOR_ADDRPORT_BUF_LEN,
                      "%s:%u", conn->address, conn->port);
         answer = buf;
         break;
       case GRD_FLAG_ORIGINAL:
         /* Actual address with port */
         addr_str = tor_addr_to_str_dup(&(tlschan->conn->real_addr));
-        tor_snprintf(buf, MAX_DESCR_LEN + 1,
-                     "%s:%u", addr_str, conn->port);
+        tor_snprintf(buf, TOR_ADDRPORT_BUF_LEN, "%s:%u", addr_str, conn->port);
         tor_free(addr_str);
         answer = buf;
         break;
diff --git a/src/lib/net/address.c b/src/lib/net/address.c
index e112da947..7f6359387 100644
--- a/src/lib/net/address.c
+++ b/src/lib/net/address.c
@@ -1188,8 +1188,7 @@ fmt_addr_impl(const tor_addr_t *addr, int decorate)
 const char *
 fmt_addrport(const tor_addr_t *addr, uint16_t port)
 {
-  /* Add space for a colon and up to 5 digits. */
-  static char buf[TOR_ADDR_BUF_LEN + 6];
+  static char buf[TOR_ADDRPORT_BUF_LEN];
   tor_snprintf(buf, sizeof(buf), "%s:%u", fmt_and_decorate_addr(addr), port);
   return buf;
 }
diff --git a/src/lib/net/address.h b/src/lib/net/address.h
index 1cf78beca..eca5ddab7 100644
--- a/src/lib/net/address.h
+++ b/src/lib/net/address.h
@@ -213,6 +213,15 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
  */
 #define TOR_ADDR_BUF_LEN 48
 
+/** Length of a buffer containing an IP address along with a port number and
+ * a seperating colon.
+ *
+ * This allows enough space for
+ *   "[ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255]:12345",
+ * plus a terminating NUL.
+ */
+#define TOR_ADDRPORT_BUF_LEN (TOR_ADDR_BUF_LEN + 6)
+
 char *tor_addr_to_str_dup(const tor_addr_t *addr) ATTR_MALLOC;
 
 /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6





More information about the tor-commits mailing list