[libcxx] r215977 - Give libcxx tests temporary filenames that are actually unique.

Jonathan Roelofs jonathan at codesourcery.com
Tue Aug 19 06:56:56 PDT 2014


Author: jroelofs
Date: Tue Aug 19 08:56:56 2014
New Revision: 215977

URL: http://llvm.org/viewvc/llvm-project?rev=215977&view=rev
Log:
Give libcxx tests temporary filenames that are actually unique.

This fixes a race condition on temp file name creation.

http://reviews.llvm.org/D4962

Modified:
    libcxx/trunk/test/support/platform_support.h

Modified: libcxx/trunk/test/support/platform_support.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/platform_support.h?rev=215977&r1=215976&r2=215977&view=diff
==============================================================================
--- libcxx/trunk/test/support/platform_support.h (original)
+++ libcxx/trunk/test/support/platform_support.h Tue Aug 19 08:56:56 2014
@@ -50,13 +50,23 @@ inline
 std::string
 get_temp_file_name()
 {
-   std::string s("temp.XXXXXX");
 #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__)
-   _mktemp(&s[0]);
+    char Path[MAX_PATH+1];
+    char FN[MAX_PATH+1];
+    do { } while (0 == GetTempPath(MAX_PATH+1, Path));
+    do { } while (0 == GetTempFileName(Path, "libcxx", 0, FN));
+    return FN;
 #else
-   mktemp(&s[0]);
+    std::string Name;
+    int FD = -1;
+    do {
+      Name = "libcxx.XXXXXX";
+      FD = mkstemp(&Name[0]);
+      assert(errno != EINVAL && "Something is wrong with the mkstemp's argument");
+    } while (FD == -1 || errno == EEXIST);
+    close(FD);
+    return Name;
 #endif
-   return s;
 }
 
 #endif // PLATFORM_SUPPORT_H





More information about the cfe-commits mailing list