@@ -12,6 +12,8 @@ Author: CM Wintersteiger
12
12
#include < windows.h>
13
13
#include < io.h>
14
14
#include < direct.h>
15
+ #else
16
+ #include < vector>
15
17
#endif
16
18
17
19
#include < cstdlib>
@@ -34,17 +36,20 @@ std::string get_temporary_directory(const std::string &name_template)
34
36
std::string result;
35
37
36
38
#ifdef _WIN32
37
- DWORD dwBufSize = MAX_PATH;
38
- char lpPathBuffer[MAX_PATH];
39
+ DWORD dwBufSize = MAX_PATH+ 1 ;
40
+ char lpPathBuffer[MAX_PATH+ 1 ];
39
41
DWORD dwRetVal = GetTempPathA (dwBufSize, lpPathBuffer);
40
42
41
43
if (dwRetVal > dwBufSize || (dwRetVal == 0 ))
42
44
throw " GetTempPath failed" ; // NOLINT(readability/throw)
43
45
44
- char t[MAX_PATH];
46
+ char t[MAX_PATH+ 1 ];
45
47
46
48
strncpy (t, name_template.c_str (), MAX_PATH);
49
+ t[MAX_PATH]=0 ; // ensure zero termination
47
50
51
+ // the below appends, but Windows doesn't produce path names
52
+ // longer than MAX_PATH
48
53
UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t);
49
54
if (uRetVal == 0 )
50
55
throw " GetTempFileName failed" ; // NOLINT(readability/throw)
@@ -64,9 +69,9 @@ std::string get_temporary_directory(const std::string &name_template)
64
69
prefixed_name_template+=' /' ;
65
70
prefixed_name_template+=name_template;
66
71
67
- char t[ 1000 ] ;
68
- strncpy (t, prefixed_name_template. c_str (), 1000 );
69
- const char *td = mkdtemp (t);
72
+ std::vector< char > t (prefixed_name_template. begin (), prefixed_name_template. end ()) ;
73
+ t. push_back ( ' \0 ' ); // add the zero
74
+ const char *td = mkdtemp (t. data () );
70
75
if (!td)
71
76
throw " mkdtemp failed" ;
72
77
result=std::string (td);
0 commit comments