@@ -15,7 +15,7 @@ Author: CM Wintersteiger
15
15
#endif
16
16
17
17
#include < cstdlib>
18
- #include < cstring >
18
+ #include < vector >
19
19
20
20
#if defined(__linux__) || \
21
21
defined (__FreeBSD_kernel__) || \
@@ -34,17 +34,18 @@ std::string get_temporary_directory(const std::string &name_template)
34
34
std::string result;
35
35
36
36
#ifdef _WIN32
37
- DWORD dwBufSize = MAX_PATH;
38
- char lpPathBuffer[MAX_PATH];
37
+ DWORD dwBufSize = MAX_PATH+ 1 ;
38
+ char lpPathBuffer[MAX_PATH+ 1 ];
39
39
DWORD dwRetVal = GetTempPathA (dwBufSize, lpPathBuffer);
40
40
41
41
if (dwRetVal > dwBufSize || (dwRetVal == 0 ))
42
42
throw " GetTempPath failed" ; // NOLINT(readability/throw)
43
43
44
- char t[MAX_PATH];
45
-
46
- strncpy (t, name_template. c_str (), MAX_PATH);
44
+ // GetTempFileNameA produces <path>\<pre><uuuu>.TMP
45
+ // where <pre> = "TLO"
46
+ // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
47
47
48
+ char t[MAX_PATH];
48
49
UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t);
49
50
if (uRetVal == 0 )
50
51
throw " GetTempFileName failed" ; // NOLINT(readability/throw)
@@ -64,9 +65,9 @@ std::string get_temporary_directory(const std::string &name_template)
64
65
prefixed_name_template+=' /' ;
65
66
prefixed_name_template+=name_template;
66
67
67
- char t[ 1000 ] ;
68
- strncpy (t, prefixed_name_template. c_str (), 1000 );
69
- const char *td = mkdtemp (t);
68
+ std::vector< char > t (prefixed_name_template. begin (), prefixed_name_template. end ()) ;
69
+ t. push_back ( ' \0 ' ); // add the zero
70
+ const char *td = mkdtemp (t. data () );
70
71
if (!td)
71
72
throw " mkdtemp failed" ;
72
73
result=std::string (td);
0 commit comments