Skip to content

Commit a43e0f9

Browse files
authored
[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()`.
1 parent b629b86 commit a43e0f9

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

libcxx/test/support/filesystem_test_helper.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,26 @@ struct scoped_test_env
320320
// allow tests to call this unguarded.
321321
#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(_WIN32)
322322
std::string create_socket(std::string file) {
323-
file = sanitize_path(std::move(file));
324-
325-
::sockaddr_un address;
326-
address.sun_family = AF_UNIX;
327-
assert(file.size() <= sizeof(address.sun_path));
328-
::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
329-
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
330-
::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address));
331-
return file;
323+
file = sanitize_path(std::move(file));
324+
325+
::sockaddr_un address;
326+
address.sun_family = AF_UNIX;
327+
328+
// If file.size() is too big, try to create a file directly inside
329+
// /tmp to make sure file path is short enough.
330+
// Android platform warns about tmpnam, since the problem does not appear
331+
// on Android, let's not apply it for Android.
332+
# if !defined(__ANDROID__)
333+
if (file.size() <= sizeof(address.sun_path)) {
334+
file = std::tmpnam(nullptr);
332335
}
336+
# endif
337+
assert(file.size() <= sizeof(address.sun_path));
338+
::strncpy(address.sun_path, file.c_str(), sizeof(address.sun_path));
339+
int fd = ::socket(AF_UNIX, SOCK_STREAM, 0);
340+
assert(::bind(fd, reinterpret_cast<::sockaddr*>(&address), sizeof(address)) == 0);
341+
return file;
342+
}
333343
#endif
334344

335345
fs::path test_root;

0 commit comments

Comments
 (0)