Fix a real temp file leak in FileOutputBuffer

If we failed to commit the buffer but did not die to a signal, the temp
file would remain on disk on Windows. Having an open file mapping and
file handle prevents the file from being deleted. I am choosing not to
add an assertion of success on the temp file removal, since virus
scanners and other environmental things can often cause removal to fail
in real world tools.

Also fix more temp file leaks in unit tests.

llvm-svn: 280445
This commit is contained in:
Reid Kleckner 2016-09-02 01:10:53 +00:00
parent d4e80a9615
commit 1a4398a198
4 changed files with 19 additions and 9 deletions

View File

@ -32,6 +32,9 @@ FileOutputBuffer::FileOutputBuffer(std::unique_ptr<mapped_file_region> R,
: Region(std::move(R)), FinalPath(Path), TempPath(TmpPath) {}
FileOutputBuffer::~FileOutputBuffer() {
// Close the mapping before deleting the temp file, so that the removal
// succeeds.
Region.reset();
sys::fs::remove(Twine(TempPath));
}

View File

@ -20,9 +20,12 @@ using namespace llvm::sys;
#define ASSERT_NO_ERROR(x) \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
errs() << #x ": did not return errc::success.\n" \
SmallString<128> MessageStorage; \
raw_svector_ostream Message(MessageStorage); \
Message << #x ": did not return errc::success.\n" \
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
} else { \
}

View File

@ -488,6 +488,8 @@ TEST_F(FileSystemTest, Unique) {
fs::createUniqueDirectory("dir2", Dir2));
ASSERT_NO_ERROR(fs::getUniqueID(Dir2.c_str(), F2));
ASSERT_NE(F1, F2);
ASSERT_NO_ERROR(fs::remove(Dir1));
ASSERT_NO_ERROR(fs::remove(Dir2));
ASSERT_NO_ERROR(fs::remove(TempPath2));
ASSERT_NO_ERROR(fs::remove(TempPath));
}

View File

@ -17,13 +17,15 @@ using namespace llvm;
#define ASSERT_NO_ERROR(x) \
if (std::error_code ASSERT_NO_ERROR_ec = x) { \
errs() << #x ": did not return errc::success.\n" \
SmallString<128> MessageStorage; \
raw_svector_ostream Message(MessageStorage); \
Message << #x ": did not return errc::success.\n" \
<< "error number: " << ASSERT_NO_ERROR_ec.value() << "\n" \
<< "error message: " << ASSERT_NO_ERROR_ec.message() << "\n"; \
GTEST_FATAL_FAILURE_(MessageStorage.c_str()); \
} else { \
}
namespace {
TEST(raw_pwrite_ostreamTest, TestSVector) {