From 0daa1da3ba0ba1dbf2892e042573887f430b32fd Mon Sep 17 00:00:00 2001 From: Neel Chauhan Date: Sun, 26 Apr 2020 13:06:11 -0700 Subject: [PATCH] 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(-) create mode 100644 changes/ticket33956 diff --git a/changes/ticket33956 b/changes/ticket33956 new file mode 100644 index 0000000000..7ad802797d --- /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 be941c1762..484727268c 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 e112da9479..7f63593875 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 1cf78beca1..eca5ddab75 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