From ec86eec7973bda62a5ebbb21eb40f7102d84b32b Mon Sep 17 00:00:00 2001 From: Brian Somers Date: Wed, 14 Mar 2001 20:50:52 +0000 Subject: [PATCH] MAXHOSTNAMELEN includes space for the NUL Don't read past the end of the host passed to realhostname() Not objected to by: freebsd-audit Interface disliked by: imp --- lib/libutil/realhostname.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/libutil/realhostname.c b/lib/libutil/realhostname.c index 26bd5e6174e..7c409a8cfe7 100644 --- a/lib/libutil/realhostname.c +++ b/lib/libutil/realhostname.c @@ -52,7 +52,7 @@ struct sockinet { int realhostname(char *host, size_t hsize, const struct in_addr *ip) { - char trimmed[MAXHOSTNAMELEN+1]; + char trimmed[MAXHOSTNAMELEN]; int result; struct hostent *hp; @@ -136,15 +136,15 @@ realhostname_sa(char *host, size_t hsize, struct sockaddr *addr, int addrlen) freeaddrinfo(ores); goto numeric; } - strncpy(buf, ores->ai_canonname, + strlcpy(buf, ores->ai_canonname, sizeof(buf)); trimdomain(buf, hsize); - strncpy(host, buf, hsize); - if (strlen(host) > hsize && + if (strlen(buf) > hsize && addr->sa_family == AF_INET) { freeaddrinfo(ores); goto numeric; } + strncpy(host, buf, hsize); break; } ((struct sockinet *)addr)->si_port = port;