Skip to content

Commit

Permalink
network: clean up address printing
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
zx2c4 committed Nov 19, 2018
1 parent b18a295 commit 5a3c103
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 5a3c103

Please sign in to comment.