From a8170d774c19a087ca0b096d0146c3153e7f6a63 Mon Sep 17 00:00:00 2001 From: Yanqin Jin Date: Thu, 4 Jun 2020 14:19:05 -0700 Subject: [PATCH] Close file to avoid file-descriptor leakage (#6936) Summary: When operation on an open file descriptor fails, we should close the file descriptor. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6936 Test Plan: make check Reviewed By: pdillinger Differential Revision: D21885458 Pulled By: riversand963 fbshipit-source-id: ba077a76b256a8537f21e22e4ec198f45390bf50 --- HISTORY.md | 1 + env/fs_posix.cc | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0edb91016..bde805fb4 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -11,6 +11,7 @@ * Let best-efforts recovery ignore corrupted files during table loading. * Fix corrupt key read from ingested file when iterator direction switches from reverse to forward at a key that is a prefix of another key in the same file. It is only possible in files with a non-zero global seqno. * Fix abnormally large estimate from GetApproximateSizes when a range starts near the end of one SST file and near the beginning of another. Now GetApproximateSizes consistently and fairly includes the size of SST metadata in addition to data blocks, attributing metadata proportionally among the data blocks based on their size. +* Fix potential file descriptor leakage in PosixEnv's IsDirectory() and NewRandomAccessFile(). ### Public API Change * Flush(..., column_family) may return Status::ColumnFamilyDropped() instead of Status::InvalidArgument() if column_family is dropped while processing the flush request. diff --git a/env/fs_posix.cc b/env/fs_posix.cc index 92808eaa2..b00db6f67 100644 --- a/env/fs_posix.cc +++ b/env/fs_posix.cc @@ -241,6 +241,8 @@ class PosixFileSystem : public FileSystem { s = IOError("while mmap file for read", fname, errno); close(fd); } + } else { + close(fd); } } else { if (options.use_direct_reads && !options.use_mmap_reads) { @@ -889,14 +891,16 @@ class PosixFileSystem : public FileSystem { if (fd < 0) { return IOError("While open for IsDirectory()", path, errno); } + IOStatus io_s; struct stat sbuf; if (fstat(fd, &sbuf) < 0) { - return IOError("While doing stat for IsDirectory()", path, errno); + io_s = IOError("While doing stat for IsDirectory()", path, errno); } - if (nullptr != is_dir) { + close(fd); + if (io_s.ok() && nullptr != is_dir) { *is_dir = S_ISDIR(sbuf.st_mode); } - return IOStatus::OK(); + return io_s; } FileOptions OptimizeForLogWrite(const FileOptions& file_options,