[libcxx-commits] [libcxx] [libc++][test] try to directly create socket file in /tmp when filepath is too long (PR #77058)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 5 02:50:40 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Wu Yingcong (yingcong-wu)

<details>
<summary>Changes</summary>

If TMP is set to a folder which path is too long, the current libcxx test helper function `create_socket()` will fail because of the test temp folder `test_root`'s path is too long to be used in socket creation.
In such case, this patch will try to create the socket file directly in `/tmp` folder.

---
Full diff: https://github.com/llvm/llvm-project/pull/77058.diff


1 Files Affected:

- (modified) libcxx/test/support/filesystem_test_helper.h (+21-8) 


``````````diff
diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index a049efe03d844e..e472eced8217a1 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -18,6 +18,7 @@
 #include <chrono>
 #include <cstdint>
 #include <cstdio> // for printf
+#include <ctime>
 #include <string>
 #include <system_error>
 #include <type_traits>
@@ -320,15 +321,27 @@ struct scoped_test_env
   // allow tests to call this unguarded.
 #if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
     std::string create_socket(std::string file) {
-        file = sanitize_path(std::move(file));
+      std::string socket_file = sanitize_path(file);
 
-        ::sockaddr_un address;
-        address.sun_family = AF_UNIX;
-        assert(file.size() <= sizeof(address.sun_path));
-        ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
-        int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
-        ::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address));
-        return file;
+      ::sockaddr_un address;
+      address.sun_family = AF_UNIX;
+
+      // If file.size() is too big, try to create a file directly inside
+      // /tmp to make sure file path is short enough.
+      if (socket_file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
+        fs::path const tmp = "/tmp";
+        std::size_t i      = std::hash<std::string>()(std::to_string(std::time(nullptr)));
+        fs::path p         = tmp / ("libcxx-socket-" + file + "-" + std::to_string(i));
+        while (utils::exists(p.string())) {
+          p = tmp / ("libcxx-socket-" + file + "-" + std::to_string(++i));
+        }
+        socket_file = p.string();
+      }
+      assert(socket_file.size() <= sizeof(address.sun_path));
+      ::strncpy(address.sun_path, socket_file.c_str(), sizeof(address.sun_path));
+      int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
+      assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
+      return socket_file;
     }
 #endif
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/77058


More information about the libcxx-commits mailing list