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

Wu Yingcong via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jan 5 02:04:28 PST 2024


https://github.com/yingcong-wu updated https://github.com/llvm/llvm-project/pull/77058

>From c4102bd48b799a88c46066071cb85c6bbe2b1607 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 00:48:34 -0800
Subject: [PATCH 1/3] try to directly create file in /tmp when filepath is too
 long

---
 libcxx/test/support/filesystem_test_helper.h | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index d63b1e61b5f9b5..948af08bc94c3d 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>
@@ -314,10 +315,22 @@ struct scoped_test_env
 
         ::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 (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));
+            }
+            file = p.string();
+        }
         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));
+        assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
         return file;
     }
 #endif

>From e8d55bdc4bd5e2fef174591d3627ed3baf7f7dbf Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 01:13:24 -0800
Subject: [PATCH 2/3] format

---
 libcxx/test/support/filesystem_test_helper.h | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 948af08bc94c3d..0cd5b4e3c2f1b0 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -319,13 +319,13 @@ struct scoped_test_env
         // If file.size() is too big, try to create a file directly inside
         // /tmp to make sure file path is short enough.
         if (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));
-            }
-            file = p.string();
+          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));
+          }
+          file = p.string();
         }
         assert(file.size() <= sizeof(address.sun_path));
         ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));

>From 0243c221053bd5a47748fd43cba1ac9a97497857 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Fri, 5 Jan 2024 01:59:22 -0800
Subject: [PATCH 3/3] fix error

---
 libcxx/test/support/filesystem_test_helper.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index 0cd5b4e3c2f1b0..7d3d4ca131f9bb 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -311,27 +311,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;
 
         // If file.size() is too big, try to create a file directly inside
         // /tmp to make sure file path is short enough.
-        if (file.size() <= sizeof(address.sun_path) && utils::exists("/tmp")) {
+        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));
           }
-          file = p.string();
+          socket_file = p.string();
         }
-        assert(file.size() <= sizeof(address.sun_path));
-        ::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
+        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 file;
+        return socket_file;
     }
 #endif
 



More information about the libcxx-commits mailing list