From owner-svn-src-stable@freebsd.org Tue Jan 12 10:24:09 2016 Return-Path: Delivered-To: svn-src-stable@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 B217CA6C84E; Tue, 12 Jan 2016 10:24:09 +0000 (UTC) (envelope-from trasz@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 692111634; Tue, 12 Jan 2016 10:24:09 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u0CAO8lJ036990; Tue, 12 Jan 2016 10:24:08 GMT (envelope-from trasz@FreeBSD.org) Received: (from trasz@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u0CAO8rt036989; Tue, 12 Jan 2016 10:24:08 GMT (envelope-from trasz@FreeBSD.org) Message-Id: <201601121024.u0CAO8rt036989@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: trasz set sender to trasz@FreeBSD.org using -f From: Edward Tomasz Napierala Date: Tue, 12 Jan 2016 10:24:08 +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: r293747 - stable/10/sbin/init 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@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jan 2016 10:24:09 -0000 Author: trasz Date: Tue Jan 12 10:24:08 2016 New Revision: 293747 URL: https://svnweb.freebsd.org/changeset/base/293747 Log: MFC r290689: Fix resource leaks in error cases. Sponsored by: The FreeBSD Foundation Modified: stable/10/sbin/init/init.c Directory Properties: stable/10/ (props changed) Modified: stable/10/sbin/init/init.c ============================================================================== --- stable/10/sbin/init/init.c Tue Jan 12 10:19:56 2016 (r293746) +++ stable/10/sbin/init/init.c Tue Jan 12 10:24:08 2016 (r293747) @@ -660,6 +660,7 @@ read_file(const char *path, void **bufp, error = fstat(fd, &sb); if (error != 0) { emergency("fstat: %s", strerror(errno)); + close(fd); return (error); } @@ -667,12 +668,14 @@ read_file(const char *path, void **bufp, buf = malloc(bufsize); if (buf == NULL) { emergency("malloc: %s", strerror(errno)); + close(fd); return (error); } nbytes = read(fd, buf, bufsize); if (nbytes != (ssize_t)bufsize) { emergency("read: %s", strerror(errno)); + close(fd); free(buf); return (error); } @@ -691,7 +694,7 @@ read_file(const char *path, void **bufp, } static int -create_file(const char *path, void *buf, size_t bufsize) +create_file(const char *path, const void *buf, size_t bufsize) { ssize_t nbytes; int error, fd; @@ -705,13 +708,13 @@ create_file(const char *path, void *buf, nbytes = write(fd, buf, bufsize); if (nbytes != (ssize_t)bufsize) { emergency("write: %s", strerror(errno)); + close(fd); return (-1); } error = close(fd); if (error != 0) { emergency("close: %s", strerror(errno)); - free(buf); return (-1); } @@ -757,6 +760,9 @@ reroot(void) size_t bufsize, init_path_len; int error, name[4]; + buf = NULL; + bufsize = 0; + name[0] = CTL_KERN; name[1] = KERN_PROC; name[2] = KERN_PROC_PATHNAME; @@ -782,12 +788,6 @@ reroot(void) } /* - * Pacify GCC. - */ - buf = NULL; - bufsize = 0; - - /* * Copy the init binary into tmpfs, so that we can unmount * the old rootfs without committing suicide. */ @@ -809,6 +809,7 @@ reroot(void) out: emergency("reroot failed; going to single user mode"); + free(buf); return (state_func_t) single_user; }