Commit 6fa6e9f7 authored by Sean McBride's avatar Sean McBride

replaced sprintf with snprintf, with larger refactorings needed

parent 6ea5cd5d
......@@ -223,8 +223,9 @@ void vtkRIBExporter::WriteHeader (vtkRenderer *aRen)
{
// create a FileName to hold the renderered image
char *imageFileName = new char [strlen (this->FilePrefix) + strlen (".tif") + 1];
sprintf (imageFileName, "%s%s", this->FilePrefix, ".tif");
size_t length = strlen (this->FilePrefix) + strlen (".tif") + 1;
char *imageFileName = new char [length];
snprintf (imageFileName, length, "%s%s", this->FilePrefix, ".tif");
fprintf (this->FilePtr, "FrameBegin %d\n", 1);
fprintf (this->FilePtr, "Display \"%s\" \"file\" \"rgb\"\n", imageFileName);
......
......@@ -64,12 +64,13 @@ void vtkRIBProperty::SetVariable (const char *variable, const char *value)
delete [] this->Declarations;
// format of line is: Declare "variable" "type"\n
this->Declarations = new char [strlen ("Declare ") +
strlen (variable) +
strlen (value) +
8];
size_t length = strlen ("Declare ") +
strlen (variable) +
strlen (value) +
8;
this->Declarations = new char [length];
sprintf (this->Declarations, "Declare \"%s\" \"%s\"\n", variable, value);
snprintf (this->Declarations, length, "Declare \"%s\" \"%s\"\n", variable, value);
this->Modified ();
}
......@@ -81,12 +82,13 @@ void vtkRIBProperty::AddVariable (const char *variable, const char *value)
}
else
{
char *newVariable = new char [strlen ("Declare ") +
strlen (variable) +
strlen (value) +
8];
size_t length = strlen ("Declare ") +
strlen (variable) +
strlen (value) +
8;
char *newVariable = new char [length];
sprintf (newVariable, "Declare \"%s\" \"%s\"\n", variable, value);
snprintf (newVariable, length, "Declare \"%s\" \"%s\"\n", variable, value);
char *oldDeclarations = this->Declarations;
this->Declarations = new char [strlen (oldDeclarations) + strlen (newVariable) + 1];
......@@ -109,11 +111,12 @@ void vtkRIBProperty::SetSurfaceShaderParameter (const char *parameter, const cha
delete [] this->SurfaceShaderParameters;
// format of line is: "parameter" "value"
this->SurfaceShaderParameters = new char [strlen (parameter) +
strlen (value) +
7];
size_t length = strlen (parameter) +
strlen (value) +
7;
this->SurfaceShaderParameters = new char [length];
sprintf (this->SurfaceShaderParameters, " \"%s\" [%s]", parameter, value);
snprintf (this->SurfaceShaderParameters, length, " \"%s\" [%s]", parameter, value);
this->Modified ();
}
......@@ -122,11 +125,12 @@ void vtkRIBProperty::SetDisplacementShaderParameter (const char *parameter, cons
delete [] this->DisplacementShaderParameters;
// format of line is: "parameter" "value"
this->DisplacementShaderParameters = new char [strlen (parameter) +
strlen (value) +
7];
size_t length = strlen (parameter) +
strlen (value) +
7;
this->DisplacementShaderParameters = new char [length];
sprintf (this->DisplacementShaderParameters, " \"%s\" [%s]", parameter, value);
snprintf (this->DisplacementShaderParameters, length, " \"%s\" [%s]", parameter, value);
this->Modified ();
}
......@@ -144,11 +148,12 @@ void vtkRIBProperty::AddSurfaceShaderParameter (const char *SurfaceShaderParamet
}
else
{
char *newSurfaceShaderParameter = new char [strlen (SurfaceShaderParameter) +
strlen (value) +
7];
size_t length = strlen (SurfaceShaderParameter) +
strlen (value) +
7;
char *newSurfaceShaderParameter = new char [length];
sprintf (newSurfaceShaderParameter, " \"%s\" [%s]", SurfaceShaderParameter, value);
snprintf (newSurfaceShaderParameter, length, " \"%s\" [%s]", SurfaceShaderParameter, value);
char *oldSurfaceShaderParameters = this->SurfaceShaderParameters;
this->SurfaceShaderParameters = new char [strlen (oldSurfaceShaderParameters) + strlen (newSurfaceShaderParameter) + 1];
......@@ -168,11 +173,12 @@ void vtkRIBProperty::AddDisplacementShaderParameter (const char *DisplacementSha
}
else
{
char *newDisplacementShaderParameter = new char [strlen (DisplacementShaderParameter) +
strlen (value) +
7];
size_t length = strlen (DisplacementShaderParameter) +
strlen (value) +
7;
char *newDisplacementShaderParameter = new char [length];
sprintf (newDisplacementShaderParameter, " \"%s\" [%s]", DisplacementShaderParameter, value);
snprintf (newDisplacementShaderParameter, length, " \"%s\" [%s]", DisplacementShaderParameter, value);
char *oldDisplacementShaderParameters = this->DisplacementShaderParameters;
this->DisplacementShaderParameters = new char [strlen (oldDisplacementShaderParameters) + strlen (newDisplacementShaderParameter) + 1];
......
......@@ -504,8 +504,6 @@ void vtkImageWriter::DeleteFiles()
{
return;
}
int i;
char *fileName;
vtkErrorMacro("Ran out of disk space; deleting file(s) already written");
......@@ -517,23 +515,24 @@ void vtkImageWriter::DeleteFiles()
{
if (this->FilePrefix)
{
fileName =
new char[strlen(this->FilePrefix) + strlen(this->FilePattern) + 10];
size_t fileNameLength = strlen(this->FilePrefix) + strlen(this->FilePattern) + 10;
char *fileName = new char[fileNameLength];
for (i = this->MinimumFileNumber; i <= this->MaximumFileNumber; i++)
for (int i = this->MinimumFileNumber; i <= this->MaximumFileNumber; i++)
{
sprintf(fileName, this->FilePattern, this->FilePrefix, i);
snprintf(fileName, fileNameLength, this->FilePattern, this->FilePrefix, i);
vtksys::SystemTools::RemoveFile(fileName);
}
delete [] fileName;
}
else
{
fileName = new char[strlen(this->FilePattern) + 10];
size_t fileNameLength = strlen(this->FilePattern) + 10;
char *fileName = new char[fileNameLength];
for (i = this->MinimumFileNumber; i <= this->MaximumFileNumber; i++)
for (int i = this->MinimumFileNumber; i <= this->MaximumFileNumber; i++)
{
sprintf(fileName, this->FilePattern, i);
snprintf(fileName, fileNameLength, this->FilePattern, i);
vtksys::SystemTools::RemoveFile(fileName);
}
delete [] fileName;
......
......@@ -424,7 +424,7 @@ int vtkNrrdReader::ReadHeader(vtkCharArray *headerBuffer)
else if (filepatterninfo.size() >=4)
{
// description should be "<format> <min> <max> <step> [<subdim>]"
// where <format> is a string to be processed by sprintf and <min>,
// where <format> is a string to be processed by snprintf and <min>,
// <max>, and <step> form the numbers. <subdim> defines on which
// dimension the files are split up.
std::string format = filepatterninfo[0];
......@@ -433,10 +433,11 @@ int vtkNrrdReader::ReadHeader(vtkCharArray *headerBuffer)
int step = atoi(filepatterninfo[3].c_str());
subDimension = ( (filepatterninfo.size() > 4)
? atoi(filepatterninfo[4].c_str()) : numDimensions );
char *filename = new char[format.size() + 20];
size_t filenamelen = format.size() + 20;
char *filename = new char[filenamelen];
for (int i = min; i <= max; i += step)
{
sprintf(filename, format.c_str(), i);
snprintf(filename, filenamelen, format.c_str(), i);
this->DataFiles->InsertNextValue(filename);
}
delete[] filename;
......
......@@ -390,11 +390,12 @@ VrmlNodeType::addField(const char *nodeName, int type)
void
VrmlNodeType::addExposedField(const char *nodeName, int type)
{
std::vector<char> tmp(20 + strlen(nodeName));
size_t length = 20 + strlen(nodeName);
std::vector<char> tmp(length);
add(fields, nodeName, type);
sprintf(&tmp[0], "set_%s", nodeName);
snprintf(&tmp[0], length, "set_%s", nodeName);
add(eventIns, &tmp[0], type);
sprintf(&tmp[0], "%s_changed", nodeName);
snprintf(&tmp[0], length, "%s_changed", nodeName);
add(eventOuts, &tmp[0], type);
};
......
......@@ -55,10 +55,11 @@ int SLACParticleReader(int argc, char *argv[])
meshReader->SetMeshFileName(meshFileName);
delete[] meshFileName;
char *modeFileName = new char[strlen(modeFileNamePattern) + 10];
size_t modeFileNameLength = strlen(modeFileNamePattern) + 10;
char *modeFileName = new char[modeFileNameLength];
for (int i = 0; i < 9; i++)
{
sprintf(modeFileName, modeFileNamePattern, i);
snprintf(modeFileName, modeFileNameLength, modeFileNamePattern, i);
meshReader->AddModeFileName(modeFileName);
}
delete[] modeFileName;
......
......@@ -96,9 +96,9 @@ void vtkDynamic2DLabelMapper::SetPriorityArrayName(const char* name)
//----------------------------------------------------------------------------
template<typename T>
void vtkDynamic2DLabelMapper_PrintComponent(char *output, const char *format, int index, const T *array)
void vtkDynamic2DLabelMapper_PrintComponent(char *output, size_t outputSize, const char *format, int index, const T *array)
{
sprintf(output, format, array[index]);
snprintf(output, outputSize, format, array[index]);
}
//----------------------------------------------------------------------------
......@@ -373,7 +373,7 @@ void vtkDynamic2DLabelMapper::RenderOpaqueGeometry(vtkViewport *viewport,
{
switch (numericData->GetDataType())
{
vtkTemplateMacro(vtkDynamic2DLabelMapper_PrintComponent(TempString, LiveFormatString, activeComp, static_cast<VTK_TT *>(rawData)));
vtkTemplateMacro(vtkDynamic2DLabelMapper_PrintComponent(TempString, sizeof(TempString), LiveFormatString, activeComp, static_cast<VTK_TT *>(rawData)));
}
ResultString = TempString;
}
......@@ -387,7 +387,7 @@ void vtkDynamic2DLabelMapper::RenderOpaqueGeometry(vtkViewport *viewport,
switch (numericData->GetDataType())
{
vtkTemplateMacro(
vtkDynamic2DLabelMapper_PrintComponent(TempString,
vtkDynamic2DLabelMapper_PrintComponent(TempString, sizeof(TempString),
LiveFormatString,
j,
static_cast<VTK_TT *>(rawData)));
......
......@@ -49,9 +49,9 @@ vtkCxxSetObjectMacro(vtkLabeledDataMapper,Transform,vtkTransform);
// ----------------------------------------------------------------------
template<typename T>
void vtkLabeledDataMapper_PrintComponent(char *output, const char *format, int index, const T *array)
void vtkLabeledDataMapper_PrintComponent(char *output, size_t outputSize, const char *format, int index, const T *array)
{
sprintf(output, format, array[index]);
snprintf(output, outputSize, format, array[index]);
}
......@@ -556,7 +556,7 @@ void vtkLabeledDataMapper::BuildLabelsInternal(vtkDataSet* input)
{
switch (numericData->GetDataType())
{
vtkTemplateMacro(vtkLabeledDataMapper_PrintComponent(TempString,
vtkTemplateMacro(vtkLabeledDataMapper_PrintComponent(TempString, sizeof(TempString),
LiveFormatString, activeComp, static_cast<VTK_TT *>(rawData)));
}
ResultString = TempString;
......@@ -572,6 +572,7 @@ void vtkLabeledDataMapper::BuildLabelsInternal(vtkDataSet* input)
{
vtkTemplateMacro(
vtkLabeledDataMapper_PrintComponent(TempString,
sizeof(TempString),
LiveFormatString,
j,
static_cast<VTK_TT *>(rawData)));
......
......@@ -238,7 +238,8 @@ void vtkLabeledTreeMapDataMapper::GetVertexLabel(vtkIdType vertex,
vtkDataArray *numericData,
vtkStringArray *stringData,
int activeComp, int numComp,
char *string)
char *string,
size_t stringSize)
{
char format[1024];
double val;
......@@ -255,13 +256,13 @@ void vtkLabeledTreeMapDataMapper::GetVertexLabel(vtkIdType vertex,
string[0] = '\0';
return;
}
sprintf(string, this->LabelFormat,
static_cast<char>(numericData->GetComponent(vertex, activeComp)));
snprintf(string, stringSize, this->LabelFormat,
static_cast<char>(numericData->GetComponent(vertex, activeComp)));
}
else
{
sprintf(string, this->LabelFormat,
numericData->GetComponent(vertex, activeComp));
snprintf(string, stringSize, this->LabelFormat,
numericData->GetComponent(vertex, activeComp));
}
}
else
......@@ -269,11 +270,11 @@ void vtkLabeledTreeMapDataMapper::GetVertexLabel(vtkIdType vertex,
strcpy(format, "("); strcat(format, this->LabelFormat);
for (j=0; j<(numComp-1); j++)
{
sprintf(string, format, numericData->GetComponent(vertex, j));
snprintf(string, stringSize, format, numericData->GetComponent(vertex, j));
strcpy(format,string); strcat(format,", ");
strcat(format, this->LabelFormat);
}
sprintf(string, format, numericData->GetComponent(vertex, numComp-1));
snprintf(string, stringSize, format, numericData->GetComponent(vertex, numComp-1));
strcat(string, ")");
}
}
......@@ -285,13 +286,13 @@ void vtkLabeledTreeMapDataMapper::GetVertexLabel(vtkIdType vertex,
string[0] = '\0';
return;
}
sprintf(string, this->LabelFormat,
stringData->GetValue(vertex).c_str());
snprintf(string, stringSize, this->LabelFormat,
stringData->GetValue(vertex).c_str());
}
else // Use the vertex id
{
val = static_cast<double>(vertex);
sprintf(string, this->LabelFormat, val);
snprintf(string, stringSize, this->LabelFormat, val);
}
}
......@@ -522,7 +523,7 @@ void vtkLabeledTreeMapDataMapper::LabelTree(vtkTree *tree,
}
this->GetVertexLabel(vertex, numericData, stringData, activeComp, numComps,
string);
string, sizeof(string));
results = this->AnalyseLabel(string, level, blimitsDC, textPosWC, &tprop);
if (results == 1)
{
......
......@@ -135,8 +135,8 @@ protected:
vtkDataArray *numericData, vtkStringArray *stringData,
int activeComp, int numComps);
void GetVertexLabel(vtkIdType vertex, vtkDataArray *numericData,
vtkStringArray *stringData, int activeComp, int numComps,
char *string);
vtkStringArray *stringData, int activeComp, int numComps,
char *string, size_t stringSize);
void UpdateFontSizes();
int UpdateWindowInfo(vtkViewport *viewport);
int GetStringSize(char *string, int level);
......
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