Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions libcxx/test/support/filesystem_test_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too happy about this, but since we assert afterwards it will fail the test. If that happens we can investigate.

# 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;
Expand Down