From 50847862ed3329482a87b2d8091a03bdaa7f4ab6 Mon Sep 17 00:00:00 2001 From: Martin Pärtel Date: Tue, 7 Feb 2023 08:53:56 +0200 Subject: Fixed FD leak when using --block-devices-as-files (#125) --- src/bindfs.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/bindfs.c b/src/bindfs.c index 263aa0e..b049679 100644 --- a/src/bindfs.c +++ b/src/bindfs.c @@ -457,25 +457,26 @@ static int getattr_common(const char *procpath, struct stat *stbuf) if (settings.block_devices_as_files && S_ISBLK(stbuf->st_mode)) { stbuf->st_mode ^= S_IFBLK | S_IFREG; // Flip both bits int fd = open(procpath, O_RDONLY); + if (fd == -1) { + return -errno; + } #ifdef __linux__ uint64_t size; ioctl(fd, BLKGETSIZE64, &size); stbuf->st_size = (off_t)size; if (stbuf->st_size < 0) { // Underflow + close(fd); return -EOVERFLOW; } #else - if (fd == -1) { - return -errno; - } off_t size = lseek(fd, 0, SEEK_END); if (size == (off_t)-1) { close(fd); return -errno; } stbuf->st_size = size; - close(fd); #endif + close(fd); } /* Then permission bits. Symlink permissions don't matter, though. */ -- cgit v1.2.3