From 30675098fce9aaa56027adc42042ea70c986fe01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kim=20Nguy=E1=BB=85n?= Date: Sat, 30 Nov 2013 09:27:36 +0100 Subject: [PATCH] Replace "unsafe" function tempnam by custom code. --- remake.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/remake.cpp b/remake.cpp index b6c3c6b..30440a8 100644 --- a/remake.cpp +++ b/remake.cpp @@ -2560,7 +2560,28 @@ static void create_server() if (sigaction(SIGINT, &sa, NULL) == -1) goto error; // Prepare a named unix socket in temporary directory. - socket_name = tempnam(NULL, "rmk-"); + struct stat tmpstat; + char * tmpdir; + if ((0 == stat(tmpdir = getenv("TMPDIR"), &tmpstat) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)P_tmpdir, &tmpstat)) + && (S_ISDIR(tmpstat.st_mode))) + || ((0 == stat(tmpdir = (char*)"/tmp", &tmpstat)) + && (S_ISDIR(tmpstat.st_mode)))); + else goto error; + std::stringstream tmpname; + long int rnd = now ^ getpid(); + do + { + srandom(rnd); + rnd = random(); + tmpname << tmpdir << "/rmk-"; + tmpname.fill('0'); + tmpname.width(8); + tmpname << rnd; + } + while (access (tmpname.str().c_str(), F_OK) == 0); + socket_name = strdup(tmpname.str().c_str()); if (!socket_name) goto error2; struct sockaddr_un socket_addr; size_t len = strlen(socket_name); -- 2.17.1