diff options
author | trasz <trasz@FreeBSD.org> | 2016-01-12 10:24:08 +0000 |
---|---|---|
committer | trasz <trasz@FreeBSD.org> | 2016-01-12 10:24:08 +0000 |
commit | 8bd7436f2ba8cb82b96bb85dc75e58ed686efebb (patch) | |
tree | 9ada5db94344fd2a7c10c14b3bdc26e525fa567d /sbin | |
parent | 3aea2923e9c1218bee7ad00506dcb6857d7eaaa4 (diff) | |
download | FreeBSD-src-8bd7436f2ba8cb82b96bb85dc75e58ed686efebb.zip FreeBSD-src-8bd7436f2ba8cb82b96bb85dc75e58ed686efebb.tar.gz |
MFC r290689:
Fix resource leaks in error cases.
Sponsored by: The FreeBSD Foundation
Diffstat (limited to 'sbin')
-rw-r--r-- | sbin/init/init.c | 17 |
1 files changed, 9 insertions, 8 deletions
diff --git a/sbin/init/init.c b/sbin/init/init.c index e7079bd..af672a9 100644 --- a/sbin/init/init.c +++ b/sbin/init/init.c @@ -660,6 +660,7 @@ read_file(const char *path, void **bufp, size_t *bufsizep) 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, size_t *bufsizep) 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, size_t *bufsizep) } 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, size_t bufsize) 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; } |