From a5929f9a8823713901e4fb9a3162faa98aa59388 Mon Sep 17 00:00:00 2001 From: yar Date: Tue, 20 Aug 2002 14:56:06 +0000 Subject: Clean up hostname and hostinfo handling in inithosts(): o check getaddrinfo(3) return value, not result pointer o getaddrinfo(3) returns int, not pointer o don't leak memory allocated for hostnames and hostinfo structures o initialize pointers that will be checked for NULL somewhere MFC after: 1 week --- libexec/ftpd/ftpd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'libexec') diff --git a/libexec/ftpd/ftpd.c b/libexec/ftpd/ftpd.c index 7b833b6..9704f60 100644 --- a/libexec/ftpd/ftpd.c +++ b/libexec/ftpd/ftpd.c @@ -681,8 +681,7 @@ inithosts(void) memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME; hints.ai_family = AF_UNSPEC; - getaddrinfo(hrp->hostname, NULL, &hints, &res); - if (res) + if (getaddrinfo(hrp->hostname, NULL, &hints, &res) == 0) hrp->hostinfo = res; hrp->statfile = _PATH_FTPDSTATFILE; hrp->welcome = _PATH_FTPWELCOME; @@ -754,8 +753,7 @@ inithosts(void) hints.ai_flags = 0; hints.ai_family = AF_UNSPEC; hints.ai_flags = AI_PASSIVE; - error = getaddrinfo(vhost, NULL, &hints, &res); - if (error != NULL) + if (getaddrinfo(vhost, NULL, &hints, &res) != 0) goto nextline; for (ai = res; ai != NULL && ai->ai_addr != NULL; ai = ai->ai_next) { @@ -779,9 +777,13 @@ inithosts(void) if (hrp == NULL) { if ((hrp = malloc(sizeof(struct ftphost))) == NULL) goto nextline; + hrp->hostname = NULL; + hrp->hostinfo = NULL; insert = 1; } else insert = 0; /* host already in the chain */ + if (hrp->hostinfo) + freeaddrinfo(hrp->hostinfo); hrp->hostinfo = res; /* @@ -823,7 +825,13 @@ inithosts(void) } } } - if ((hrp->hostname = strdup(vhost)) == NULL) + if (hrp->hostname && + strcmp(hrp->hostname, vhost) != 0) { + free(hrp->hostname); + hrp->hostname = NULL; + } + if (hrp->hostname == NULL && + (hrp->hostname = strdup(vhost)) == NULL) goto nextline; hrp->anonuser = anonuser; hrp->statfile = statfile; -- cgit v1.1