From ef5120c189617e82674f4da9f55ab7e413dbee57 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Tue, 11 Jun 2019 16:42:42 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363083 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/Path.cpp | 1 + unittests/Support/Path.cpp | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 5312e1df3b6..c49260125db 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -1125,6 +1125,7 @@ TempFile &TempFile::operator=(TempFile &&Other) { TmpName = std::move(Other.TmpName); FD = Other.FD; Other.Done = true; + Other.FD = -1; return *this; } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 4eee8e923b7..f0e11b4e3f6 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -578,6 +578,7 @@ TEST_F(FileSystemTest, TempFileKeepDiscard) { 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, TempFileDiscardDiscard) { 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")); -- 2.40.0