-
Notifications
You must be signed in to change notification settings - Fork 285
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,7 @@ Author: CM Wintersteiger | |
| #endif | ||
|
|
||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <vector> | ||
|
|
||
| #if defined(__linux__) || \ | ||
| defined(__FreeBSD_kernel__) || \ | ||
|
|
@@ -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]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation of
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the
cstringinclude still required for non-win32?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually the
_WIN32part needs the very same fix, and thencstringisn't required at all anymore in here.There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.