Skip to content
Snippets Groups Projects
Commit 20eb7ae5 authored by Jan David Mol's avatar Jan David Mol
Browse files

bug 1362: check return codes of fgets/mktemps, and prevent memory corruption due to strcpy

parent 44ee4548
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <cerrno>
#include <climits> #include <climits>
#if defined(__APPLE__) #if defined(__APPLE__)
...@@ -92,7 +93,8 @@ int remoteCopy (const string& localFile, ...@@ -92,7 +93,8 @@ int remoteCopy (const string& localFile,
else { else {
// construct the error message // construct the error message
while(!feof(f)) { while(!feof(f)) {
fgets(outputLine,200,f); if(!fgets(outputLine,200,f))
break;
if(!feof(f)) { if(!feof(f)) {
outputString+=string(outputLine); outputString+=string(outputLine);
} }
...@@ -144,7 +146,8 @@ int copyFromRemote(const string& remoteHost, ...@@ -144,7 +146,8 @@ int copyFromRemote(const string& remoteHost,
else { else {
// construct the error message // construct the error message
while(!feof(f)) { while(!feof(f)) {
fgets(outputLine,200,f); if(!fgets(outputLine,200,f))
break;
if(!feof(f)) { if(!feof(f)) {
outputString+=string(outputLine); outputString+=string(outputLine);
} }
...@@ -170,13 +173,24 @@ string getTempFileName(const string& format) ...@@ -170,13 +173,24 @@ string getTempFileName(const string& format)
char tempFileName [256]; char tempFileName [256];
if (format.find("XXXXXX", 0) != string::npos) { // did user specify mask? if (format.find("XXXXXX", 0) != string::npos) { // did user specify mask?
strcpy (tempFileName, format.c_str()); // use user-mask strncpy (tempFileName, format.c_str(), sizeof tempFileName); // use user-mask
} }
else { else {
strcpy (tempFileName, "/tmp/LOFAR_XXXXXX");// use default mask strncpy (tempFileName, "/tmp/LOFAR_XXXXXX", sizeof tempFileName); // use default mask
} }
mkstemp(tempFileName); // let OS invent the name // note that if we actually cut off the mask, it will likely be invalid because
// it has to end in XXXXXX
tempFileName[sizeof tempFileName - 1] = 0;
if (!mkstemp(tempFileName)) { // let OS invent the name
if(errno == EINVAL)
LOG_ERROR(formatString("Invalid temporary file-name mask %s (specified %s)",tempFileName,format.c_str()));
else
LOG_ERROR(formatString("Could not create temporary file with mask %s (specified %s)",tempFileName,format.c_str()));
return "";
}
return string(tempFileName); return string(tempFileName);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment