Commit b6a0ea04 authored by Sean McBride's avatar Sean McBride

Replace unsafe C string functions with safer varieties

strcpy->strncpy
sprintf->snprintf
parent 5f15eaa6
......@@ -71,6 +71,10 @@ POSSIBILITY OF SUCH DAMAGES.
#include <map>
#include <sstream>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
//-------------------------------------------------------------------------
// A container for mapping attribute names to arrays
class vtkMINCImageAttributeMap
......@@ -315,11 +319,11 @@ const char *vtkMINCImageAttributes::ConvertDataArrayToString(
char storage[128];
if (dataType == VTK_DOUBLE)
{
sprintf(storage, "%0.15g", val);
snprintf(storage, 128, "%0.15g", val);
}
else
{
sprintf(storage, "%0.7g", val);
snprintf(storage, 128, "%0.7g", val);
}
// Add a decimal if there isn't one, to distinguish from int
for (char *cp = storage; *cp != '.'; cp++)
......@@ -869,11 +873,13 @@ void vtkMINCImageAttributes::SetAttributeValueAsString(
const char *attribute,
const char *value)
{
size_t length = strlen(value)+1;
size_t length = strlen(value);
vtkCharArray *array = vtkCharArray::New();
array->SetNumberOfValues(length);
strcpy(array->GetPointer(0), value);
array->SetNumberOfValues(length+1);
char *dest = array->GetPointer(0);
strncpy(dest, value, length);
dest[length] = '\0';
this->SetAttributeValueAsArray(variable, attribute, array);
......
......@@ -87,6 +87,10 @@ POSSIBILITY OF SUCH DAMAGES.
#include <vector>
#include <map>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#define VTK_MINC_MAX_DIMS 8
//--------------------------------------------------------------------------
......@@ -437,7 +441,7 @@ std::string vtkMINCImageWriterCreateIdentString()
#else
int processId = getpid();
#endif
sprintf(buf, "%i%s%i", processId, itemsep, identx++);
snprintf(buf, 1024, "%i%s%i", processId, itemsep, identx++);
ident.append(buf);
return ident;
......
......@@ -189,7 +189,8 @@ int vtkMNITagPointReader::ReadLineAfterComments(
{
delete [] this->Comments;
this->Comments = new char[comments.length() + 1];
strcpy(this->Comments, comments.c_str());
strncpy(this->Comments, comments.c_str(), comments.length());
this->Comments[comments.length()] = '\0';
return 1;
}
......
......@@ -205,7 +205,8 @@ int vtkMNITransformReader::ReadLineAfterComments(
{
delete [] this->Comments;
this->Comments = new char[comments.length() + 1];
strcpy(this->Comments, comments.c_str());
strncpy(this->Comments, comments.c_str(), comments.length());
this->Comments[comments.length()] = '\0';
return 1;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment