From owner-svn-src-head@freebsd.org Fri Nov 20 22:36:43 2015 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 3EB16A34B7E; Fri, 20 Nov 2015 22:36:43 +0000 (UTC) (envelope-from rpokala@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 E10911FE5; Fri, 20 Nov 2015 22:36:42 +0000 (UTC) (envelope-from rpokala@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id tAKMaf9X048448; Fri, 20 Nov 2015 22:36:41 GMT (envelope-from rpokala@FreeBSD.org) Received: (from rpokala@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id tAKMaf06048447; Fri, 20 Nov 2015 22:36:41 GMT (envelope-from rpokala@FreeBSD.org) Message-Id: <201511202236.tAKMaf06048447@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: rpokala set sender to rpokala@FreeBSD.org using -f From: Ravi Pokala Date: Fri, 20 Nov 2015 22:36:41 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r291114 - head/lib/libc/gen 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.20 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: Fri, 20 Nov 2015 22:36:43 -0000 Author: rpokala Date: Fri Nov 20 22:36:41 2015 New Revision: 291114 URL: https://svnweb.freebsd.org/changeset/base/291114 Log: popen() requires check for fdopen() failure Move fdopen() up near other resource allocation like malloc(); do proper deallocation on failure later on in the function. Submitted by: Ramachandra Topannavar Reviewed by: jilles Approved by: jhb (mentor) MFC after: 2 weeks Sponsored by: Panasas, Inc. Differential Revision: https://reviews.freebsd.org/D4126 M lib/libc/gen/popen.c Modified: head/lib/libc/gen/popen.c Modified: head/lib/libc/gen/popen.c ============================================================================== --- head/lib/libc/gen/popen.c Fri Nov 20 21:56:20 2015 (r291113) +++ head/lib/libc/gen/popen.c Fri Nov 20 22:36:41 2015 (r291114) @@ -72,6 +72,7 @@ popen(const char *command, const char *t struct pid *cur; FILE *iop; int pdes[2], pid, twoway, cloexec; + int pdes_unused_in_parent; char *argv[4]; struct pid *p; @@ -98,6 +99,20 @@ popen(const char *command, const char *t return (NULL); } + if (*type == 'r') { + iop = fdopen(pdes[0], type); + pdes_unused_in_parent = pdes[1]; + } else { + iop = fdopen(pdes[1], type); + pdes_unused_in_parent = pdes[0]; + } + if (iop == NULL) { + (void)_close(pdes[0]); + (void)_close(pdes[1]); + free(cur); + return (NULL); + } + argv[0] = "sh"; argv[1] = "-c"; argv[2] = (char *)command; @@ -107,9 +122,14 @@ popen(const char *command, const char *t switch (pid = vfork()) { case -1: /* Error. */ THREAD_UNLOCK(); - (void)_close(pdes[0]); - (void)_close(pdes[1]); + /* + * The _close() closes the unused end of pdes[], while + * the fclose() closes the used end of pdes[], *and* cleans + * up iop. + */ + (void)_close(pdes_unused_in_parent); free(cur); + (void)fclose(iop); return (NULL); /* NOTREACHED */ case 0: /* Child. */ @@ -145,14 +165,8 @@ popen(const char *command, const char *t } THREAD_UNLOCK(); - /* Parent; assume fdopen can't fail. */ - if (*type == 'r') { - iop = fdopen(pdes[0], type); - (void)_close(pdes[1]); - } else { - iop = fdopen(pdes[1], type); - (void)_close(pdes[0]); - } + /* Parent. */ + (void)_close(pdes_unused_in_parent); /* Link into list of file descriptors. */ cur->fp = iop;