Fix the "dangerous use of tempnam" warning in Host/SocketTest.cpp

instead, create a unique temporary directory, and place the socket file
there.

llvm-svn: 349495
This commit is contained in:
Pavel Labath 2018-12-18 15:33:50 +00:00
parent ef87c26796
commit 56279cf24f
1 changed files with 10 additions and 9 deletions

View File

@ -17,6 +17,8 @@
#include "lldb/Host/Socket.h"
#include "lldb/Host/common/TCPSocket.h"
#include "lldb/Host/common/UDPSocket.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#ifndef LLDB_DISABLE_POSIX
#include "lldb/Host/posix/DomainSocket.h"
@ -41,7 +43,6 @@ public:
protected:
static void AcceptThread(Socket *listen_socket,
const char *listen_remote_address,
bool child_processes_inherit, Socket **accept_socket,
Status *error) {
*error = listen_socket->Accept(*accept_socket);
@ -49,7 +50,7 @@ protected:
template <typename SocketType>
void CreateConnectedSockets(
const char *listen_remote_address,
llvm::StringRef listen_remote_address,
const std::function<std::string(const SocketType &)> &get_connect_addr,
std::unique_ptr<SocketType> *a_up, std::unique_ptr<SocketType> *b_up) {
bool child_processes_inherit = false;
@ -64,8 +65,8 @@ protected:
Status accept_error;
Socket *accept_socket;
std::thread accept_thread(AcceptThread, listen_socket_up.get(),
listen_remote_address, child_processes_inherit,
&accept_socket, &accept_error);
child_processes_inherit, &accept_socket,
&accept_error);
std::string connect_remote_address = get_connect_addr(*listen_socket_up);
std::unique_ptr<SocketType> connect_socket_up(
@ -158,15 +159,15 @@ TEST_F(SocketTest, DecodeHostAndPort) {
#ifndef LLDB_DISABLE_POSIX
TEST_F(SocketTest, DomainListenConnectAccept) {
char *file_name_str = tempnam(nullptr, nullptr);
EXPECT_NE(nullptr, file_name_str);
const std::string file_name(file_name_str);
free(file_name_str);
llvm::SmallString<64> Path;
std::error_code EC = llvm::sys::fs::createUniqueDirectory("DomainListenConnectAccept", Path);
ASSERT_FALSE(EC);
llvm::sys::path::append(Path, "test");
std::unique_ptr<DomainSocket> socket_a_up;
std::unique_ptr<DomainSocket> socket_b_up;
CreateConnectedSockets<DomainSocket>(
file_name.c_str(), [=](const DomainSocket &) { return file_name; },
Path, [=](const DomainSocket &) { return Path.str().str(); },
&socket_a_up, &socket_b_up);
}
#endif