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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jan 9 11:15:47 PST 2024


Author: Wu Yingcong
Date: 2024-01-09T20:15:44+01:00
New Revision: a43e0f90b650fdcdf80bcb221d50a62905bf8977

URL: https://github.com/llvm/llvm-project/commit/a43e0f90b650fdcdf80bcb221d50a62905bf8977
DIFF: https://github.com/llvm/llvm-project/commit/a43e0f90b650fdcdf80bcb221d50a62905bf8977.diff

LOG: [libc++][test] try to directly create socket file in /tmp when filepath is too long (#77058)

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.

This patch also add an assertion for `bind()`.

Added: 
    

Modified: 
    libcxx/test/support/filesystem_test_helper.h

Removed: 
    


################################################################################
diff  --git a/libcxx/test/support/filesystem_test_helper.h b/libcxx/test/support/filesystem_test_helper.h
index a049efe03d844e..2ff237a4b6220c 100644
--- a/libcxx/test/support/filesystem_test_helper.h
+++ b/libcxx/test/support/filesystem_test_helper.h
@@ -320,16 +320,26 @@ 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));
-
-        ::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;
+      file = sanitize_path(std::move(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.
+// Android platform warns about tmpnam, since the problem does not appear
+// on Android, let's not apply it for Android.
+#  if !defined(__ANDROID__)
+    if (file.size() <= sizeof(address.sun_path)) {
+      file = std::tmpnam(nullptr);
     }
+#  endif
+    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);
+    assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
+    return file;
+  }
 #endif
 
     fs::path test_root;


        


More information about the libcxx-commits mailing list