]> xenbits.xensource.com Git - people/liuw/freebsd.git/commitdiff
Fix resource leaks in error cases.
authortrasz <trasz@FreeBSD.org>
Wed, 11 Nov 2015 23:00:57 +0000 (23:00 +0000)
committertrasz <trasz@FreeBSD.org>
Wed, 11 Nov 2015 23:00:57 +0000 (23:00 +0000)
MFC after: 1 month
Sponsored by: The FreeBSD Foundation

sbin/init/init.c

index 082650767ab3878647ad9e7359323c73d2bf505f..0827f33232f223778563d690661807aae4f3ac5f 100644 (file)
@@ -659,6 +659,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);
        }
 
@@ -666,12 +667,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);
        }
@@ -690,7 +693,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;
@@ -704,13 +707,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);
        }
 
@@ -756,6 +759,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;
@@ -780,12 +786,6 @@ reroot(void)
                goto out;
        }
 
-       /*
-        * Pacify GCC.
-        */
-       buf = NULL;
-       bufsize = 0;
-
        /*
         * Copy the init binary into tmpfs, so that we can unmount
         * the old rootfs without committing suicide.
@@ -808,6 +808,7 @@ reroot(void)
 
 out:
        emergency("reroot failed; going to single user mode");
+       free(buf);
        return (state_func_t) single_user;
 }