From owner-svn-src-stable-10@freebsd.org Sat Jan 9 05:05:16 2016 Return-Path: Delivered-To: svn-src-stable-10@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 D1C27A69810; Sat, 9 Jan 2016 05:05:16 +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 8448B1F55; Sat, 9 Jan 2016 05:05:16 +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 u0955FDq030226; Sat, 9 Jan 2016 05:05:15 GMT (envelope-from rpokala@FreeBSD.org) Received: (from rpokala@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u0955FYL030225; Sat, 9 Jan 2016 05:05:15 GMT (envelope-from rpokala@FreeBSD.org) Message-Id: <201601090505.u0955FYL030225@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: rpokala set sender to rpokala@FreeBSD.org using -f From: Ravi Pokala Date: Sat, 9 Jan 2016 05:05:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org Subject: svn commit: r293463 - stable/10/lib/libc/gen X-SVN-Group: stable-10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-10@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for only the 10-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 09 Jan 2016 05:05:16 -0000 Author: rpokala Date: Sat Jan 9 05:05:15 2016 New Revision: 293463 URL: https://svnweb.freebsd.org/changeset/base/293463 Log: MFC r291114: 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. Approved by: jhb Sponsored by: Panasas, Inc. Modified: stable/10/lib/libc/gen/popen.c Directory Properties: stable/10/ (props changed) Modified: stable/10/lib/libc/gen/popen.c ============================================================================== --- stable/10/lib/libc/gen/popen.c Sat Jan 9 05:02:35 2016 (r293462) +++ stable/10/lib/libc/gen/popen.c Sat Jan 9 05:05:15 2016 (r293463) @@ -73,6 +73,7 @@ popen(command, type) struct pid *cur; FILE *iop; int pdes[2], pid, twoway, cloexec; + int pdes_unused_in_parent; char *argv[4]; struct pid *p; @@ -99,6 +100,20 @@ popen(command, type) 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; @@ -108,9 +123,14 @@ popen(command, type) 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. */ @@ -155,14 +175,8 @@ popen(command, type) } 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;