Skip to content

Commit c6cbf7c

Browse files
author
Daniel Kroening
authored
Merge pull request diffblue#2147 from diffblue/fix-tempdir-buffer-overflow
fix potential non-zero termination of a string buffer
2 parents 41d7a45 + 9609a52 commit c6cbf7c

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

src/goto-cc/compile.cpp

+1-7
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,14 @@ bool compilet::add_files_from_archive(
226226
const std::string &file_name,
227227
bool thin_archive)
228228
{
229-
#ifdef _WIN32
230-
char td[MAX_PATH + 1];
231-
#else
232-
char td[] = "goto-cc.XXXXXX";
233-
#endif
234-
235229
std::stringstream cmd;
236230
FILE *stream;
237231

238232
std::string tstr = working_directory;
239233

240234
if(!thin_archive)
241235
{
242-
tstr = get_temporary_directory(td);
236+
tstr = get_temporary_directory("goto-cc.XXXXXX");
243237

244238
if(tstr=="")
245239
{

src/util/tempdir.cpp

+10-9
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)