From 5a3c103505f7a84ffd154ab0b73dbd237b91e6a4 Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Mon, 19 Nov 2018 04:33:03 +0100 Subject: [PATCH] network: clean up address printing clang-analyzer complains about sockaddr_storage being uninitalized, so we zero that out first. Then, while we're at it, we use the right constants for getnameinfo output sizes, and note the fact that the null byte is part of snprintf's calculations. Signed-off-by: Jason A. Donenfeld --- network.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/network.c b/network.c index a32d173..602dc33 100644 --- a/network.c +++ b/network.c @@ -37,7 +37,8 @@ char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen) { - static char buf[1100], address[1025], port[32]; + static char buf[NI_MAXHOST + NI_MAXSERV + 4]; + char address[NI_MAXHOST], port[NI_MAXSERV]; int err; err = getnameinfo(addr, addrlen, address, sizeof(address), @@ -48,9 +49,9 @@ char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen) log_printf_exit(1, log_err, "getnameinfo: %s", gai_strerror(err)); if (addr->sa_family == AF_INET6) - snprintf(buf, sizeof(buf) - 1, "[%s]:%s", address, port); + snprintf(buf, sizeof(buf), "[%s]:%s", address, port); else - snprintf(buf, sizeof(buf) - 1, "%s:%s", address, port); + snprintf(buf, sizeof(buf), "%s:%s", address, port); return buf; } @@ -262,7 +263,7 @@ int accept_connections(int listening_sockets[]) for (i = 0; listening_sockets[i] != -1; i++) { int listen_sock; - struct sockaddr_storage client_addr; + struct sockaddr_storage client_addr = { 0 }; socklen_t addrlen = sizeof(client_addr); if (!FD_ISSET(listening_sockets[i], &readfds))