From 10719a6ff8e8af4202634d40fa5bf888e1ff6fcd Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 7 Sep 2022 05:36:26 +0900 Subject: [PATCH] loop-util: fix leak of file descriptor on failure --- src/shared/loop-util.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index ed237c6f20..ba818d5371 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -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(