close open FDs on error, and use error path in more situations
authorAlan T. DeKok <aland@freeradius.org>
Tue, 14 Mar 2017 23:30:02 +0000 (19:30 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 14 Mar 2017 23:45:09 +0000 (19:45 -0400)
src/main/command.c

index e9d635a..34c5268 100644 (file)
@@ -392,7 +392,9 @@ static int fr_server_domain_socket_perm(char const *path, uid_t uid, gid_t gid)
                fr_strerror_printf("Failed determining parent directory");
        error:
                talloc_free(dir);
-               close(dir_fd);
+               if (sock_fd >= 0) close(sock_fd);
+               if (dir_fd >= 0) close(dir_fd);
+               if (parent_fd >= 0) close(parent_fd);
                return -1;
        }
 
@@ -458,7 +460,7 @@ static int fr_server_domain_socket_perm(char const *path, uid_t uid, gid_t gid)
                if (ret < 0) {
                        fr_strerror_printf("Failed changing ownership of control socket directory: %s",
                                           fr_syserror(errno));
-                       return -1;
+                       goto error;
                }
        /*
         *      Control socket dir already exists, but we still need to
@@ -526,7 +528,7 @@ static int fr_server_domain_socket_perm(char const *path, uid_t uid, gid_t gid)
                if (client_fd >= 0) {
                        fr_strerror_printf("Control socket '%s' is already in use", path);
                        close(client_fd);
-                       return -1;
+                       goto error;
                }
        }
 
@@ -675,7 +677,8 @@ static int fr_server_domain_socket_perm(char const *path, uid_t uid, gid_t gid)
        if (uid != (uid_t)-1) rad_seuid(euid);
        if (gid != (gid_t)-1) rad_segid(egid);
 
-       close(dir_fd);
+       if (dir_fd >= 0) close(dir_fd);
+       if (parent_fd >= 0) close(parent_fd);
 
        return sock_fd;
 }