diff mbox series

[committed] libstdc++: Make "nonexistent" paths less predictable in filesystem tests

Message ID YCabKeRT42fqIWfu@redhat.com
State New
Headers show
Series [committed] libstdc++: Make "nonexistent" paths less predictable in filesystem tests | expand

Commit Message

Jonathan Wakely Feb. 12, 2021, 3:13 p.m. UTC
The helper function for creating new paths doesn't work well on Windows,
because the PID of a process started by Wine is very consistent and so
the same path gets created each time.

libstdc++-v3/ChangeLog:

	* testsuite/util/testsuite_fs.h (nonexistent_path): Add
	random number to the path.

Tested x86_64-linux and x86_64-w64-mingw32. Committed to trunk.
commit 4179ec107943bea360b8aa75e29e2c5ad9f13e9e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Feb 12 15:13:02 2021

    libstdc++: Make "nonexistent" paths less predictable in filesystem tests
    
    The helper function for creating new paths doesn't work well on Windows,
    because the PID of a process started by Wine is very consistent and so
    the same path gets created each time.
    
    libstdc++-v3/ChangeLog:
    
            * testsuite/util/testsuite_fs.h (nonexistent_path): Add
            random number to the path.
diff mbox series

Patch

diff --git a/libstdc++-v3/testsuite/util/testsuite_fs.h b/libstdc++-v3/testsuite/util/testsuite_fs.h
index 4cda0eefeaf..e4d04dd7799 100644
--- a/libstdc++-v3/testsuite/util/testsuite_fs.h
+++ b/libstdc++-v3/testsuite/util/testsuite_fs.h
@@ -34,8 +34,13 @@  namespace test_fs = std::experimental::filesystem;
 #include <fstream>
 #include <string>
 #include <cstdio>
-#include <stdlib.h>
-#include <unistd.h>
+
+#if defined(_GNU_SOURCE) || _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
+#include <stdlib.h> // mkstemp
+#include <unistd.h> // unlink, close
+#else
+#include <random>   // std::random_device
+#endif
 
 namespace __gnu_test
 {
@@ -121,13 +126,13 @@  namespace __gnu_test
     if (file.length() > 64)
       file.resize(64);
     char buf[128];
-    static int counter;
+    static unsigned counter = std::random_device{}();
 #if _GLIBCXX_USE_C99_STDIO
     std::snprintf(buf, 128,
 #else
     std::sprintf(buf,
 #endif
-      "filesystem-test.%d.%lu-%s", counter++, (unsigned long) ::getpid(),
+      "filesystem-test.%u.%lu-%s", counter++, (unsigned long) ::getpid(),
       file.c_str());
     p = buf;
 #endif