[llvm] r280445 - Fix a real temp file leak in FileOutputBuffer

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 18:10:53 PDT 2016


Author: rnk
Date: Thu Sep  1 20:10:53 2016
New Revision: 280445

URL: http://llvm.org/viewvc/llvm-project?rev=280445&view=rev
Log:
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.

Modified:
    llvm/trunk/lib/Support/FileOutputBuffer.cpp
    llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
    llvm/trunk/unittests/Support/Path.cpp
    llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp

Modified: llvm/trunk/lib/Support/FileOutputBuffer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FileOutputBuffer.cpp?rev=280445&r1=280444&r2=280445&view=diff
==============================================================================
--- llvm/trunk/lib/Support/FileOutputBuffer.cpp (original)
+++ llvm/trunk/lib/Support/FileOutputBuffer.cpp Thu Sep  1 20:10:53 2016
@@ -32,6 +32,9 @@ FileOutputBuffer::FileOutputBuffer(std::
     : 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));
 }
 

Modified: llvm/trunk/unittests/Support/FileOutputBufferTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/FileOutputBufferTest.cpp?rev=280445&r1=280444&r2=280445&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/FileOutputBufferTest.cpp (original)
+++ llvm/trunk/unittests/Support/FileOutputBufferTest.cpp Thu Sep  1 20:10:53 2016
@@ -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"                           \
-           << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"           \
-           << "error message: " << ASSERT_NO_ERROR_ec.message() << "\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 {                                                                     \
   }
 
@@ -57,9 +60,9 @@ TEST(FileOutputBuffer, Test) {
   ASSERT_EQ(File1Size, 8192ULL);
   ASSERT_NO_ERROR(fs::remove(File1.str()));
 
- 	// TEST 2: Verify abort case.
+  // TEST 2: Verify abort case.
   SmallString<128> File2(TestDirectory);
-	File2.append("/file2");
+  File2.append("/file2");
   {
     ErrorOr<std::unique_ptr<FileOutputBuffer>> Buffer2OrErr =
         FileOutputBuffer::create(File2, 8192);

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=280445&r1=280444&r2=280445&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Thu Sep  1 20:10:53 2016
@@ -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));
 }

Modified: llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp?rev=280445&r1=280444&r2=280445&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp (original)
+++ llvm/trunk/unittests/Support/raw_pwrite_stream_test.cpp Thu Sep  1 20:10:53 2016
@@ -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"                           \
-           << "error number: " << ASSERT_NO_ERROR_ec.value() << "\n"           \
-           << "error message: " << ASSERT_NO_ERROR_ec.message() << "\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) {




More information about the llvm-commits mailing list