[llvm] r363083 - [Path] Set FD to -1 in moved-from TempFile

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 11 09:42:42 PDT 2019


Author: jdevlieghere
Date: Tue Jun 11 09:42:42 2019
New Revision: 363083

URL: http://llvm.org/viewvc/llvm-project?rev=363083&view=rev
Log:
[Path] Set FD to -1 in moved-from TempFile

When moving a temp file, explicitly set the file descriptor to -1 so we
can never accidentally close the moved-from TempFile.

Differential revision: https://reviews.llvm.org/D63087

Modified:
    llvm/trunk/lib/Support/Path.cpp
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=363083&r1=363082&r2=363083&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Tue Jun 11 09:42:42 2019
@@ -1125,6 +1125,7 @@ TempFile &TempFile::operator=(TempFile &
   TmpName = std::move(Other.TmpName);
   FD = Other.FD;
   Other.Done = true;
+  Other.FD = -1;
   return *this;
 }
 

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=363083&r1=363082&r2=363083&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Tue Jun 11 09:42:42 2019
@@ -578,6 +578,7 @@ TEST_F(FileSystemTest, TempFileKeepDisca
   auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
   ASSERT_TRUE((bool)TempFileOrError);
   fs::TempFile File = std::move(*TempFileOrError);
+  ASSERT_EQ(-1, TempFileOrError->FD);
   ASSERT_FALSE((bool)File.keep(TestDirectory + "/keep"));
   ASSERT_FALSE((bool)File.discard());
   ASSERT_TRUE(fs::exists(TestDirectory + "/keep"));
@@ -589,6 +590,7 @@ TEST_F(FileSystemTest, TempFileDiscardDi
   auto TempFileOrError = fs::TempFile::create(TestDirectory + "/test-%%%%");
   ASSERT_TRUE((bool)TempFileOrError);
   fs::TempFile File = std::move(*TempFileOrError);
+  ASSERT_EQ(-1, TempFileOrError->FD);
   ASSERT_FALSE((bool)File.discard());
   ASSERT_FALSE((bool)File.discard());
   ASSERT_FALSE(fs::exists(TestDirectory + "/keep"));




More information about the llvm-commits mailing list