[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 01:13:37 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/2] 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/2] 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));



More information about the libcxx-commits mailing list