From: Yu Watanabe Date: Tue, 6 Sep 2022 20:36:26 +0000 (+0900) Subject: loop-util: fix leak of file descriptor on failure X-Git-Tag: v252-rc1~241 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=10719a6ff8e8af4202634d40fa5bf888e1ff6fcd;p=thirdparty%2Fsystemd.git loop-util: fix leak of file descriptor on failure --- diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index ed237c6f20d..ba818d5371f 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(