[llvm] r186423 - On error, close the temporary file descriptor.

Rafael Espindola rafael.espindola at gmail.com
Tue Jul 16 09:00:32 PDT 2013


Author: rafael
Date: Tue Jul 16 11:00:32 2013
New Revision: 186423

URL: http://llvm.org/viewvc/llvm-project?rev=186423&view=rev
Log:
On error, close the temporary file descriptor.

With this change llvm-ar can remove the temporary file on windows too.

Added:
    llvm/trunk/test/Object/archive-error-tmp.txt
Modified:
    llvm/trunk/tools/llvm-ar/llvm-ar.cpp

Added: llvm/trunk/test/Object/archive-error-tmp.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Object/archive-error-tmp.txt?rev=186423&view=auto
==============================================================================
--- llvm/trunk/test/Object/archive-error-tmp.txt (added)
+++ llvm/trunk/test/Object/archive-error-tmp.txt Tue Jul 16 11:00:32 2013
@@ -0,0 +1,9 @@
+REQUIRES: shell
+
+Test that no temporary file is left behind on error.
+
+RUN: rm -rf %t
+RUN: mkdir %t
+RUN: not llvm-ar rc %t/foo.a .
+RUN: rmdir %t
+

Modified: llvm/trunk/tools/llvm-ar/llvm-ar.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ar/llvm-ar.cpp?rev=186423&r1=186422&r2=186423&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ar/llvm-ar.cpp (original)
+++ llvm/trunk/tools/llvm-ar/llvm-ar.cpp Tue Jul 16 11:00:32 2013
@@ -41,10 +41,13 @@ using namespace llvm;
 static StringRef ToolName;
 
 static const char *TemporaryOutput;
+static int TmpArchiveFD = -1;
 
 // fail - Show the error message and exit.
 LLVM_ATTRIBUTE_NORETURN static void fail(Twine Error) {
   outs() << ToolName << ": " << Error << ".\n";
+  if (TmpArchiveFD != -1)
+    close(TmpArchiveFD);
   if (TemporaryOutput)
     sys::fs::remove(TemporaryOutput);
   exit(1);
@@ -526,7 +529,6 @@ static void printWithSpacePadding(raw_os
 
 static void performWriteOperation(ArchiveOperation Operation,
                                   object::Archive *OldArchive) {
-  int TmpArchiveFD;
   SmallString<128> TmpArchive;
   failIfError(sys::fs::createUniqueFile(ArchiveName + ".temp-archive-%%%%%%%.a",
                                         TmpArchiveFD, TmpArchive));





More information about the llvm-commits mailing list