Skip to content

fix potential non-zero termination of a string buffer #2147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 1 addition & 7 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,14 @@ bool compilet::add_files_from_archive(
const std::string &file_name,
bool thin_archive)
{
#ifdef _WIN32
char td[MAX_PATH + 1];
#else
char td[] = "goto-cc.XXXXXX";
#endif

std::stringstream cmd;
FILE *stream;

std::string tstr = working_directory;

if(!thin_archive)
{
tstr = get_temporary_directory(td);
tstr = get_temporary_directory("goto-cc.XXXXXX");

if(tstr=="")
{
Expand Down
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>
#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
Member 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