@@ -12,6 +12,8 @@ Author: CM Wintersteiger
1212#include < windows.h>
1313#include < io.h>
1414#include < direct.h>
15+ #else
16+ #include < vector>
1517#endif
1618
1719#include < cstdlib>
@@ -34,17 +36,20 @@ std::string get_temporary_directory(const std::string &name_template)
3436 std::string result;
3537
3638 #ifdef _WIN32
37- DWORD dwBufSize = MAX_PATH;
38- char lpPathBuffer[MAX_PATH];
39+ DWORD dwBufSize = MAX_PATH+ 1 ;
40+ char lpPathBuffer[MAX_PATH+ 1 ];
3941 DWORD dwRetVal = GetTempPathA (dwBufSize, lpPathBuffer);
4042
4143 if (dwRetVal > dwBufSize || (dwRetVal == 0 ))
4244 throw " GetTempPath failed" ; // NOLINT(readability/throw)
4345
44- char t[MAX_PATH];
46+ char t[MAX_PATH+ 1 ];
4547
4648 strncpy (t, name_template.c_str (), MAX_PATH);
49+ t[MAX_PATH]=0 ; // ensure zero termination
4750
51+ // the below appends, but Windows doesn't produce path names
52+ // longer than MAX_PATH
4853 UINT uRetVal=GetTempFileNameA (lpPathBuffer, " TLO" , 0 , t);
4954 if (uRetVal == 0 )
5055 throw " GetTempFileName failed" ; // NOLINT(readability/throw)
@@ -64,9 +69,9 @@ std::string get_temporary_directory(const std::string &name_template)
6469 prefixed_name_template+=' /' ;
6570 prefixed_name_template+=name_template;
6671
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 () );
7075 if (!td)
7176 throw " mkdtemp failed" ;
7277 result=std::string (td);
0 commit comments