Skip to content

Commit 5907f68

Browse files
author
Daniel Kroening
committed
fix potential non-zero termination of a string buffer
1 parent 41d7a45 commit 5907f68

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/util/tempdir.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Author: CM Wintersteiger
1515
#endif
1616

1717
#include <cstdlib>
18-
#include <cstring>
18+
#include <vector>
1919

2020
#if defined(__linux__) || \
2121
defined(__FreeBSD_kernel__) || \
@@ -34,17 +34,18 @@ std::string get_temporary_directory(const std::string &name_template)
3434
std::string result;
3535

3636
#ifdef _WIN32
37-
DWORD dwBufSize = MAX_PATH;
38-
char lpPathBuffer[MAX_PATH];
37+
DWORD dwBufSize = MAX_PATH+1;
38+
char lpPathBuffer[MAX_PATH+1];
3939
DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);
4040

4141
if(dwRetVal > dwBufSize || (dwRetVal == 0))
4242
throw "GetTempPath failed"; // NOLINT(readability/throw)
4343

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.
4747

48+
char t[MAX_PATH];
4849
UINT uRetVal=GetTempFileNameA(lpPathBuffer, "TLO", 0, t);
4950
if(uRetVal == 0)
5051
throw "GetTempFileName failed"; // NOLINT(readability/throw)
@@ -64,9 +65,9 @@ std::string get_temporary_directory(const std::string &name_template)
6465
prefixed_name_template+='/';
6566
prefixed_name_template+=name_template;
6667

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());
7071
if(!td)
7172
throw "mkdtemp failed";
7273
result=std::string(td);

0 commit comments

Comments
 (0)