Skip to content

Commit 1f18227

Browse files
committed
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 <[email protected]>
1 parent 55dd9f8 commit 1f18227

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

network.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen)
3939
{
40-
static char buf[1100], address[1025], port[32];
40+
static char buf[NI_MAXHOST + NI_MAXSERV + 4];
41+
char address[NI_MAXHOST], port[NI_MAXSERV];
4142
int err;
4243

4344
err = getnameinfo(addr, addrlen, address, sizeof(address),
@@ -48,9 +49,9 @@ char *print_addr_port(const struct sockaddr *addr, socklen_t addrlen)
4849
log_printf_exit(1, log_err, "getnameinfo: %s", gai_strerror(err));
4950

5051
if (addr->sa_family == AF_INET6)
51-
snprintf(buf, sizeof(buf) - 1, "[%s]:%s", address, port);
52+
snprintf(buf, sizeof(buf), "[%s]:%s", address, port);
5253
else
53-
snprintf(buf, sizeof(buf) - 1, "%s:%s", address, port);
54+
snprintf(buf, sizeof(buf), "%s:%s", address, port);
5455

5556
return buf;
5657
}
@@ -262,7 +263,7 @@ int accept_connections(int listening_sockets[])
262263

263264
for (i = 0; listening_sockets[i] != -1; i++) {
264265
int listen_sock;
265-
struct sockaddr_storage client_addr;
266+
struct sockaddr_storage client_addr = { 0 };
266267
socklen_t addrlen = sizeof(client_addr);
267268

268269
if (!FD_ISSET(listening_sockets[i], &readfds))

0 commit comments

Comments
 (0)