]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
loop-util: fix leak of file descriptor on failure
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Sep 2022 20:36:26 +0000 (05:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 6 Sep 2022 22:52:32 +0000 (07:52 +0900)
src/shared/loop-util.c

index ed237c6f20d7e93ee00bc1438802ec98af39e322..ba818d5371fc962d50a55493e38275edff207660 100644 (file)
@@ -118,17 +118,19 @@ static int device_has_block_children(sd_device *d) {
 }
 
 static int open_lock_fd(int primary_fd, int operation) {
-        int lock_fd;
+        _cleanup_close_ int lock_fd = -1;
 
         assert(primary_fd >= 0);
+        assert(IN_SET(operation & ~LOCK_NB, LOCK_SH, LOCK_EX));
 
         lock_fd = fd_reopen(primary_fd, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
         if (lock_fd < 0)
                 return lock_fd;
+
         if (flock(lock_fd, operation) < 0)
                 return -errno;
 
-        return lock_fd;
+        return TAKE_FD(lock_fd);
 }
 
 static int loop_configure(