From owner-svn-src-head@freebsd.org Wed May 11 18:03:52 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 57D5EB37658; Wed, 11 May 2016 18:03:52 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1013A1114; Wed, 11 May 2016 18:03:51 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u4BI3pdY058153; Wed, 11 May 2016 18:03:51 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u4BI3pio058152; Wed, 11 May 2016 18:03:51 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201605111803.u4BI3pio058152@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: "Conrad E. Meyer" Date: Wed, 11 May 2016 18:03:51 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r299476 - head/usr.bin/whois X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 May 2016 18:03:52 -0000 Author: cem Date: Wed May 11 18:03:51 2016 New Revision: 299476 URL: https://svnweb.freebsd.org/changeset/base/299476 Log: whois(1): Fix potential double-close and logic mistakes Close the fd the poll error was detected on, rather than the last opened fd, to fix the double-close. Use -1 to make it explict which int variables no longer own socket file descriptors. Actually shrink, rather than grow, the poll timeout to match comment. Reported by: Coverity CID: 1304860, 1305616 Sponsored by: EMC / Isilon Storage Division Modified: head/usr.bin/whois/whois.c Modified: head/usr.bin/whois/whois.c ============================================================================== --- head/usr.bin/whois/whois.c Wed May 11 17:57:26 2016 (r299475) +++ head/usr.bin/whois/whois.c Wed May 11 18:03:51 2016 (r299476) @@ -316,6 +316,11 @@ connect_to_any_host(struct addrinfo *hos fds[i].fd = s; fds[i].events = POLLERR | POLLHUP | POLLIN | POLLOUT; + /* + * From here until a socket connects, the + * socket fd is owned by the fds[] poll array. + */ + s = -1; count++; i++; } else { @@ -357,7 +362,7 @@ connect_to_any_host(struct addrinfo *hos * after a new host have been added. */ if (timeout >= 3) - timeout <<= 1; + timeout >>= 1; break; } else if (n < 0) { @@ -377,7 +382,7 @@ connect_to_any_host(struct addrinfo *hos fds[j].revents == 0) continue; if (fds[j].revents & ~(POLLIN | POLLOUT)) { - close(s); + close(fds[j].fd); fds[j].fd = -1; fds[j].events = 0; count--; @@ -385,6 +390,7 @@ connect_to_any_host(struct addrinfo *hos } else if (fds[j].revents & (POLLIN | POLLOUT)) { /* Connect succeeded. */ s = fds[j].fd; + fds[j].fd = -1; goto done; } @@ -401,7 +407,7 @@ connect_to_any_host(struct addrinfo *hos done: /* Close all watched fds except the succeeded one */ for (j = 0; j < i; j++) - if (fds[j].fd != s && fds[j].fd != -1) + if (fds[j].fd != -1) close(fds[j].fd); free(fds); return (s);