]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Define and use TOR_ADDRPORT_BUF_LEN
authorNeel Chauhan <neel@neelc.org>
Sun, 26 Apr 2020 20:06:11 +0000 (13:06 -0700)
committerteor <teor@torproject.org>
Thu, 30 Apr 2020 12:21:48 +0000 (22:21 +1000)
changes/ticket33956 [new file with mode: 0644]
src/core/or/channeltls.c
src/lib/net/address.c
src/lib/net/address.h

diff --git a/changes/ticket33956 b/changes/ticket33956
new file mode 100644 (file)
index 0000000..7ad8027
--- /dev/null
@@ -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.
index 5cedd9fbca29a82d58c597d446a95e2c4d4c28c3..a39d32606b6189f4eb9ae0e75220973ba166e314 100644 (file)
@@ -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;
index 5dbef6a79da75958ba71fccf61d17aea9b8c597f..901dc187aef833cbd691bf1c5349aec2424e544c 100644 (file)
@@ -1173,8 +1173,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;
 }
index 498449493906aadad1b8ccb8d2f988be953ac288..7b087ce125ec48cccb7b254cc03c9a5837c025f3 100644 (file)
@@ -209,6 +209,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