sd-dhcp-server: use close_and_replace (avoid fd leak)
authorMike Yuan <me@yhndnzj.com>
Fri, 12 Apr 2024 18:40:22 +0000 (02:40 +0800)
committerMike Yuan <me@yhndnzj.com>
Fri, 12 Apr 2024 18:48:52 +0000 (02:48 +0800)
Follow-up for 11b88419ae0004547a0724aa459ddcb5d243f25c

Fixes #32252

src/libsystemd-network/sd-dhcp-server.c

index 427ed63e5b10f2d3d381be97df3380a1c10f2387..c3b0f82dc4f87dfd4a69446241d636f888b54d01 100644 (file)
@@ -1605,8 +1605,7 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
         if (!path_is_safe(path))
                 return -EINVAL;
 
-        _cleanup_close_ int fd = -EBADF;
-        fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
+        _cleanup_close_ int fd = fd_reopen(dir_fd, O_CLOEXEC | O_DIRECTORY | O_PATH);
         if (fd < 0)
                 return fd;
 
@@ -1614,6 +1613,7 @@ int sd_dhcp_server_set_lease_file(sd_dhcp_server *server, int dir_fd, const char
         if (r < 0)
                 return r;
 
-        server->lease_dir_fd = TAKE_FD(fd);
+        close_and_replace(server->lease_dir_fd, fd);
+
         return 0;
 }