Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/util/tempdir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Author: CM Wintersteiger
#endif

#include <cstdlib>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the cstring include still required for non-win32?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the _WIN32 part needs the very same fix, and then cstring isn't required at all anymore in here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetTempFileNameA actually appends to the string!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. What does it append to which string? According to the documentation it will, given the current code, produce lpPathBuffer\TLO<some digits>.TMP.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to vector on Windows as well.

#include <cstring>
#include <vector>

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

#ifdef _WIN32
DWORD dwBufSize = MAX_PATH;
char lpPathBuffer[MAX_PATH];
DWORD dwBufSize = MAX_PATH+1;
char lpPathBuffer[MAX_PATH+1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation of GetTempFileName says that it is a precondition that the path not be longer than MAX_PATH–14. Hence those changes seem unnecessary.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; but that may be subtle. The documentation of GetTempPathA says that the maximal return value is MAX_PATH+1; this is the length of the buffer required.

DWORD dwRetVal = GetTempPathA(dwBufSize, lpPathBuffer);

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

char t[MAX_PATH];

strncpy(t, name_template.c_str(), MAX_PATH);
// GetTempFileNameA produces <path>\<pre><uuuu>.TMP
// where <pre> = "TLO"
// Thus, we must make the buffer 1+3+4+1+3=12 characters longer.

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

char t[1000];
strncpy(t, prefixed_name_template.c_str(), 1000);
const char *td = mkdtemp(t);
std::vector<char> t(prefixed_name_template.begin(), prefixed_name_template.end());
t.push_back('\0'); // add the zero
const char *td = mkdtemp(t.data());
if(!td)
throw "mkdtemp failed";
result=std::string(td);
Expand Down