@@ -12,10 +12,11 @@ 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>
18
- #include < cstring>
19
20
20
21
#if defined(__linux__) || \
21
22
defined (__FreeBSD_kernel__) || \
@@ -34,26 +35,30 @@ std::string get_temporary_directory(const std::string &name_template)
34
35
std::string result;
35
36
36
37
#ifdef _WIN32
37
- DWORD dwBufSize = MAX_PATH;
38
- char lpPathBuffer[MAX_PATH];
38
+ DWORD dwBufSize = MAX_PATH+ 1 ;
39
+ char lpPathBuffer[MAX_PATH+ 1 ];
39
40
DWORD dwRetVal = GetTempPathA (dwBufSize, lpPathBuffer);
40
41
41
42
if (dwRetVal > dwBufSize || (dwRetVal == 0 ))
42
43
throw " GetTempPath failed" ; // NOLINT(readability/throw)
43
44
44
- char t[MAX_PATH];
45
+ // GetTempFileNameA produces <path>\<pre><uuuu>.TMP
46
+ // where <pre> = "TLO"
47
+ // Thus, we must make the buffer 1+3+4+1+3=12 characters longer.
45
48
46
- strncpy (t, name_template.c_str (), MAX_PATH);
49
+ std::size_t size=strlen (lpPathBuffer)+1 ;
50
+ std::vector<char > t (lpPathBuffer, lpPathBuffer+size);
51
+ t.resize (size+12 );
47
52
48
- UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t);
53
+ UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t. data () );
49
54
if (uRetVal == 0 )
50
55
throw " GetTempFileName failed" ; // NOLINT(readability/throw)
51
56
52
- unlink (t);
53
- if (_mkdir (t)!=0 )
57
+ unlink (t. data () );
58
+ if (_mkdir (t. data () )!=0 )
54
59
throw " _mkdir failed" ;
55
60
56
- result=std::string (t);
61
+ result=std::string (t. data () );
57
62
58
63
#else
59
64
std::string prefixed_name_template=" /tmp/" ;
@@ -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