Commit 6a56978e authored by Ben Boeckel's avatar Ben Boeckel Committed by Code Review

Merge topic 'cppcheck-20140919' into master

87935c2b TestIncrementalOctreePointLocator: remove dead code
898b425b vtkOpenGLPolyDataMapper2D: comment possible divide-by-zero
a59bd8c8 vtkReebGraph: add a comment about realloc usage
529dccb1 vtkParse: address cppcheck messages
ae3fa6e9 vtkParse: regenerate lex.yy.c
d6d6e8fc TestIncrementalOctreePointLocator: use malloc
79abddf1 vtkWrapHierarchy: handle realloc errors properly
86e47ae8 vtkParsePreprocess: fix up some free() logic for params
8aa578f3 vtkParsePreprocess: handle realloc errors properly
d28c44a6 vtkParseMain: close the input file
9aef0e71 vtkParseMain: handle realloc errors properly
f4e756f9 vtkResliceCursorRepresentation: avoid divide-by-zero
b0bd0465 vtkImagePlaneWidget: avoid divide-by-zero
e736ad42 vtkEnSightWriter: terminate buffers properly
b4b8206b vtkPLY: handle realloc errors properly
9a78aa1a vtkSLCReader: plug memory leak
...
parents 0ac359a1 87935c2b
......@@ -363,43 +363,49 @@ double vtkKdNode::_GetDistance2ToBoundary(
int first = 1;
minDistance = VTK_FLOAT_MAX; // Suppresses warning message.
dist = x - xmin;
if ((xmin != outerBoundaryMin[0]) &&
(((dist = x - xmin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 0;
minDistance = dist;
first = 0;
}
dist = xmax - x;
if ((xmax != outerBoundaryMax[0]) &&
(((dist = xmax - x) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 1;
minDistance = dist;
first = 0;
}
dist = y - ymin;
if ((ymin != outerBoundaryMin[1]) &&
(((dist = y - ymin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 2;
minDistance = dist;
first = 0;
}
dist = ymax - y;
if ((ymax != outerBoundaryMax[1]) &&
(((dist = ymax - y) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 3;
minDistance = dist;
first = 0;
}
dist = z - zmin;
if ((zmin != outerBoundaryMin[2]) &&
(((dist = z - zmin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 4;
minDistance = dist;
first = 0;
}
dist = zmax - z;
if ((zmax != outerBoundaryMax[2]) &&
(((dist = zmax - z) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 5;
minDistance = dist;
......
......@@ -366,43 +366,49 @@ double vtkOctreePointLocatorNode::_GetDistance2ToBoundary(
int first = 1;
minDistance = VTK_FLOAT_MAX; // Suppresses warning message.
dist = x - xmin;
if ((xmin != outerBoundaryMin[0]) &&
(((dist = x - xmin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 0;
minDistance = dist;
first = 0;
}
dist = xmax - x;
if ((xmax != outerBoundaryMax[0]) &&
(((dist = xmax - x) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 1;
minDistance = dist;
first = 0;
}
dist = y - ymin;
if ((ymin != outerBoundaryMin[1]) &&
(((dist = y - ymin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 2;
minDistance = dist;
first = 0;
}
dist = ymax - y;
if ((ymax != outerBoundaryMax[1]) &&
(((dist = ymax - y) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 3;
minDistance = dist;
first = 0;
}
dist = z - zmin;
if ((zmin != outerBoundaryMin[2]) &&
(((dist = z - zmin) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 4;
minDistance = dist;
first = 0;
}
dist = zmax - z;
if ((zmax != outerBoundaryMax[2]) &&
(((dist = zmax - z) < minDistance) || first))
((dist < minDistance) || first))
{
mindim = 5;
minDistance = dist;
......
......@@ -2601,7 +2601,7 @@ int vtkPolyhedron::InternalContour(double value,
// incoming edge in the ceBackupMap.
std::vector<vtkIdVectorType> polygonVector;
vtkIdToIdVectorMapType::iterator ceMapIt, ceBackupMapIt;
vtkIdSetType::iterator cpSetIt;
vtkIdSetType::iterator cpSetIt = cpSet.end();
vtkIdVectorType::iterator cpVectorIt;
// backup ceMap. During graph travasal, we will remove edges from contour point
......
......@@ -117,7 +117,13 @@ if (a->ArcDwId1) this->GetArc(a->ArcDwId1)->ArcUpId1=a->ArcUpId1;\
if (nstack==mstack)\
{\
mstack=vtkReebGraphMax(128,mstack*2);\
int *oldstack = stack;\
stack=(int*)realloc(stack,sizeof(int)*mstack);\
if (!stack)\
{\
free(oldstack);\
assert(0 && "Ran out of memory");\
}\
} \
stack[nstack++]=(N);\
}
......
......@@ -124,8 +124,8 @@ void Cell::replaceNode (Node* oldNode, Node* newNode)
vector<Node*>::iterator it = std::find (_nodes.begin(), _nodes.end(), oldNode);
if (it != _nodes.end())
{
_nodes.erase (it);
_nodes.insert (it, newNode);
_nodes.erase (it);
oldNode->unregisterCell (this);
newNode->registerCell (this);
}
......
......@@ -42,9 +42,7 @@ int main( int, char *[] )
vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
iren->SetRenderWindow( renWin);
planeSource->Update();
vtkPlaneSource* planeSource;
vtkPlaneSource* planeSource = vtkPlaneSource::New();
planeSource->Update();
vtkPolyDataMapper* planeSourceMapper = vtkPolyDataMapper::New();
......
......@@ -2660,6 +2660,11 @@ namespace vtkYoungsMaterialInterfaceCellCutInternals
REAL x = newtonSearchPolynomialFunc( surfaceFunction, derivatives[s], y, xmin, xmax );
DBG_MESG( "final x = "<< x );
FREE_LOCAL_ARRAY( derivatives, REAL2, nv-1 );
FREE_LOCAL_ARRAY( index, unsigned char, nv );
FREE_LOCAL_ARRAY( rindex, unsigned char, nv );
return x ;
}
......@@ -3025,6 +3030,11 @@ namespace vtkYoungsMaterialInterfaceCellCutInternals
REAL x = newtonSearchPolynomialFunc( volumeFunction, derivatives[s], y, xmin, xmax );
DBG_MESG( "final x = "<< x );
FREE_LOCAL_ARRAY( rindex, unsigned char, nv );
FREE_LOCAL_ARRAY( index, unsigned char, nv );
FREE_LOCAL_ARRAY( derivatives, REAL3, nv-1 );
return x ;
}
......
......@@ -1495,6 +1495,7 @@ int vtkUnstructuredGridGeometryFilter::RequestData(
// conn->Delete();
output->Squeeze();
delete [] cellVis;
return 1;
}
......
......@@ -2923,6 +2923,7 @@ int *vtkPKdTree::CollectLocalRegionProcessData()
if ( (regionId < 0) || (regionId >= numRegions))
{
VTKERROR("CollectLocalRegionProcessData - corrupt data");
delete [] cellCounts;
return NULL;
}
cellCounts[regionId]++;
......
......@@ -1586,6 +1586,7 @@ vtkFloatArray **
{
vtkErrorMacro(<<
"vtkDistributedDataFilter::ExchangeIdArrays memory allocation");
delete [] recvSize;
delete [] recvArrays;
return NULL;
}
......@@ -1707,6 +1708,8 @@ vtkIdTypeArray **
{
vtkErrorMacro(<<
"vtkDistributedDataFilter::ExchangeIdArrays memory allocation");
delete [] sendSize;
delete [] recvSize;
return NULL;
}
mpiContr->NoBlockReceive(recvArrays[source], recvSize[source], source, tag, req);
......
......@@ -723,14 +723,14 @@ void vtkKMeansStatistics::SelectAssessFunctor( vtkTable* inData,
return;
}
vtkKMeansAssessFunctor* kmfunc = vtkKMeansAssessFunctor::New();
if ( ! this->DistanceFunctor )
{
vtkErrorMacro( "Distance functor is NULL" );
return;
}
vtkKMeansAssessFunctor* kmfunc = vtkKMeansAssessFunctor::New();
if ( ! kmfunc->Initialize( inData, reqModel, this->DistanceFunctor ) )
{
delete kmfunc;
......
......@@ -172,7 +172,9 @@ public:
void SetFileName( char * fileName ) { this->FileName = fileName; }
const char* GetParticleName(char* variableName)
{
return( GetSeparatedParticleName(std::string(variableName)).c_str() );
static std::string particleName;
particleName = GetSeparatedParticleName(std::string(variableName));
return particleName.c_str();
}
void ReadMetaData();
......
......@@ -2125,7 +2125,7 @@ std::string vtkExodusIIWriter::CreateNameForScalarArray(
s = s.substr (0, MAX_STR_LENGTH - 11);
}
// assume largest for 32 bit decimal representation
char n[10];
char n[11];
sprintf (n, "%10d", component);
s.append (n);
return s;
......
......@@ -1207,7 +1207,7 @@ static int cgmCgmPic(cgmImagePtr im, int sticky)
headerp = static_cast<unsigned char *>(calloc(1024, SIZEOF(unsigned char )));
if (!tb || !headerp)
{
free(fb);
free(tb);
free(headerp);
return 0; /* memory allocation failed */
}
......
......@@ -449,14 +449,17 @@ int vtkNIFTIImageReader::RequestInformation(
// this checks for .hdr and .hdr.gz, case insensitive
if (vtkNIFTIImageReader::CheckExtension(filename, ".hdr"))
{
headers++;
hdrname = new char[strlen(filename) + 1];
strcpy(hdrname, filename);
if (++headers < 2)
{
hdrname = new char[strlen(filename) + 1];
strcpy(hdrname, filename);
}
}
}
if (n != 2 || headers != 1)
{
vtkErrorMacro("There must be two files and one must be a .hdr file.");
delete [] hdrname;
return 0;
}
}
......@@ -1104,6 +1107,7 @@ int vtkNIFTIImageReader::RequestData(
if (n != 2 || headers != 1)
{
vtkErrorMacro("There must be two files and one must be a .hdr file.");
delete [] imgname;
return 0;
}
}
......
......@@ -472,6 +472,7 @@ void vtkSLCReader::ExecuteDataWithInformation(vtkDataObject *output_do,
default:
vtkErrorMacro(<< "Unknown SLC compression type: " <<
data_compression );
fclose(fp);
return;
}
void* outputSlice = output->GetScalarPointer(0, 0, z_counter);
......
......@@ -1816,7 +1816,14 @@ char **vtkPLY::get_words(FILE *fp, int *nwords, char **orig_line)
/* save pointer to beginning of word */
if (num_words >= max_words) {
max_words += 10;
char** oldwords = words;
words = (char **) realloc (words, sizeof (char *) * max_words);
if (!words) {
*nwords = 0;
*orig_line = NULL;
free(oldwords);
return NULL;
}
}
words[num_words++] = ptr;
......
......@@ -905,17 +905,19 @@ void vtkEnSightWriter::WriteSOSCaseFile(int numProcs)
//----------------------------------------------------------------------------
void vtkEnSightWriter::WriteStringToFile(const char* cstring, FILE* file)
{
char cbuffer[80];
char cbuffer[81];
// Terminate the buffer to avoid static analyzer warnings about strncpy not
// NUL-terminating its destination buffer in case the input is too long.
cbuffer[80] = '\0';
strncpy(cbuffer,cstring,80);
// Write a constant 80 bytes to the file.
fwrite(cbuffer, sizeof(char),80,file);
}
//----------------------------------------------------------------------------
void vtkEnSightWriter::WriteTerminatedStringToFile(const char* cstring, FILE* file)
{
char cbuffer[512];
strncpy(cbuffer,cstring,512);
fwrite(cbuffer, sizeof(char),strlen(cbuffer),file);
fwrite(cstring, sizeof(char),std::min(strlen(cstring), static_cast<size_t>(512)),file);
}
//----------------------------------------------------------------------------
......
......@@ -1716,8 +1716,8 @@ void vtkImagePlaneWidget::UpdatePlane()
}
}
double outputSpacingX = (planeSizeX == 0) ? 1.0 : planeSizeX/extentX;
double outputSpacingY = (planeSizeY == 0) ? 1.0 : planeSizeY/extentY;
double outputSpacingX = (extentX == 0) ? 1.0 : planeSizeX/extentX;
double outputSpacingY = (extentY == 0) ? 1.0 : planeSizeY/extentY;
this->Reslice->SetOutputSpacing(outputSpacingX, outputSpacingY, 1);
this->Reslice->SetOutputOrigin(0.5*outputSpacingX, 0.5*outputSpacingY, 0);
this->Reslice->SetOutputExtent(0, extentX-1, 0, extentY-1, 0, 0);
......
......@@ -533,8 +533,8 @@ void vtkResliceCursorRepresentation::UpdateReslicePlane()
}
}
double outputSpacingX = (planeSizeX == 0) ? 1.0 : planeSizeX/extentX;
double outputSpacingY = (planeSizeY == 0) ? 1.0 : planeSizeY/extentY;
double outputSpacingX = (extentX == 0) ? 1.0 : planeSizeX/extentX;
double outputSpacingY = (extentY == 0) ? 1.0 : planeSizeY/extentY;
bool modify = false;
for (int i = 0; i < 4; i++)
......
......@@ -319,10 +319,12 @@ void vtkOpenGLPolyDataMapper2D::SetCameraShaderParameters(
vtkMatrix4x4 *tmpMat = vtkMatrix4x4::New();
tmpMat->SetElement(0,0,2.0/(right - left));
tmpMat->SetElement(1,1,2.0/(top - bottom));
// XXX(cppcheck): possible division by zero
tmpMat->SetElement(2,2,-2.0/(farV - nearV));
tmpMat->SetElement(3,3,1.0);
tmpMat->SetElement(0,3,-1.0*(right+left)/(right-left));
tmpMat->SetElement(1,3,-1.0*(top+bottom)/(top-bottom));
// XXX(cppcheck): possible division by zero
tmpMat->SetElement(2,3,-1.0*(farV+nearV)/(farV-nearV));
tmpMat->Transpose();
program->SetUniformMatrix("WCVCMatrix", tmpMat);
......
This diff is collapsed.
......@@ -13,8 +13,11 @@ Modify lex.yy.c:
- convert tabs to spaces (8 spaces per tab)
- remove extra space from end of lines
- remove blank lines from end of file
- replace "int yyl" with "yy_size_t yyl"
- replace "int yyl;" with "yy_size_t yyl;"
- compile with gcc and "-Wsign-compare", there should be no warnings
- replace "#line" lines with blank lines
- remove usage of the "register" keyword
- add a prototype for isatty
*/
......@@ -601,6 +604,7 @@ const char *get_macro_arguments()
if (c1 != '(')
{
unput(c1);
free(cp);
return NULL;
}
......@@ -654,7 +658,13 @@ const char *get_macro_arguments()
cp[i++] = ' ';
if (i >= 4 && (i & (i-1)) == 0)
{
char *oldcp = cp;
cp = (char *)realloc(cp, 2*i);
if (!cp)
{
free(oldcp);
return NULL;
}
}
}
if (sl)
......@@ -663,7 +673,13 @@ const char *get_macro_arguments()
cp[i++] = '/';
if (i >= 4 && (i & (i-1)) == 0)
{
char *oldcp = cp;
cp = (char *)realloc(cp, 2*i);
if (!cp)
{
free(oldcp);
return NULL;
}
}
}
if (c1 == '\"' || c1 == '\'')
......@@ -693,7 +709,13 @@ const char *get_macro_arguments()
cp[i++] = (char)c1;
if (i >= 4 && (i & (i-1)) == 0)
{
char *oldcp = cp;
cp = (char *)realloc(cp, 2*i);
if (!cp)
{
free(oldcp);
return NULL;
}
}
}
while (c1 != c2 || escaped);
......@@ -703,7 +725,13 @@ const char *get_macro_arguments()
cp[i++] = (char)c1;
if (i >= 4 && (i & (i-1)) == 0)
{
char *oldcp = cp;
cp = (char *)realloc(cp, 2*i);
if (!cp)
{
free(oldcp);
return NULL;
}
}
cp[i] = '\0';
if (c1 == '(')
......@@ -926,7 +954,13 @@ int skip_conditional_block()
if (i >= linemaxlen-5)
{
linemaxlen += i+5;
char *oldlinebuf = linebuf;
linebuf = (char *)realloc(linebuf, linemaxlen);
if (!linebuf)
{
free(oldlinebuf);
return 0;
}
}
linebuf[i++] = c;
/* be sure to skip escaped newlines */
......@@ -1075,6 +1109,11 @@ const char *raw_string(const char *begin)
{
m += 1024;
result = (char *)realloc(result, m);
if (!result)
{
print_preprocessor_error(VTK_PARSE_OUT_OF_MEMORY, NULL, 0);
exit(1);
}
dp = result + j;
}
......@@ -1161,6 +1200,11 @@ void push_buffer()
{
buffer_stack = (YY_BUFFER_STATE *)realloc(
buffer_stack, 2*n*sizeof(YY_BUFFER_STATE));
if (!buffer_stack)
{
print_preprocessor_error(VTK_PARSE_OUT_OF_MEMORY, NULL, 0);
exit(1);
}
}
buffer_stack[buffer_stack_size++] = YY_CURRENT_BUFFER;
}
......@@ -1214,8 +1258,18 @@ void push_include(const char *filename)
{
include_stack = (FileInfo **)realloc(
include_stack, 2*n*sizeof(FileInfo *));
if (!include_stack)
{
print_preprocessor_error(VTK_PARSE_OUT_OF_MEMORY, NULL, 0);
exit(1);
}
lineno_stack = (int *)realloc(
lineno_stack, 2*n*sizeof(int));
if (!lineno_stack)
{
print_preprocessor_error(VTK_PARSE_OUT_OF_MEMORY, NULL, 0);
exit(1);
}
}
lineno_stack[include_stack_size] = yyget_lineno();
......@@ -1287,6 +1341,11 @@ void push_macro(MacroInfo *macro)
{
macro_stack = (MacroInfo **)realloc(
macro_stack, 2*n*sizeof(MacroInfo *));
if (!macro_stack)
{
print_preprocessor_error(VTK_PARSE_OUT_OF_MEMORY, NULL, 0);
exit(1);
}
}
macro_stack[macro_stack_size++] = macro;
if (macro)
......@@ -1369,6 +1428,9 @@ void print_preprocessor_error(int result, const char *cp, size_t n)
case VTK_PARSE_SYNTAX_ERROR:
text = "syntax error";
break;
case VTK_PARSE_OUT_OF_MEMORY:
text = "out of memory";
break;
}
/* be silent about missing include files */
......
This diff is collapsed.
......@@ -18,14 +18,15 @@
This file must be translated to C and modified to build everywhere.
Run yacc like this:
Run bison like this:
yacc -b vtkParse vtkParse.y
bison -b vtkParse vtkParse.y
Modify vtkParse.tab.c:
- convert TABs to spaces (eight per tab)
- remove spaces from ends of lines, s/ *$//g
- replace all instances of "static inline" with "static".
- replace "#line" lines with blank lines
*/
/*
......@@ -416,6 +417,11 @@ void addCommentLine(const char *line, size_t n)
{
commentAllocatedLength = commentAllocatedLength + commentLength + n + 2;
commentText = (char *)realloc(commentText, commentAllocatedLength);
if (!commentText)
{
fprintf(stderr, "Wrapping: out of memory\n");
exit(1);
}
}
if (n > 0)
......
......@@ -123,7 +123,14 @@ static int read_option_file(
while (n == maxlen-1 && line[n-1] != '\n' && !feof(fp))
{
maxlen *= 2;
char *oldline = line;
line = (char *)realloc(line, maxlen);
if (!line)
{
free(oldline);
fclose(fp);
return 0;
}
if (!fgets(&line[n], (int)(maxlen-n), fp)) { break; }
n += strlen(&line[n]);
}
......@@ -210,6 +217,7 @@ static int read_option_file(
}
}
fclose(fp);
return 1;
}
......
......@@ -276,7 +276,13 @@ static MacroInfo **preproc_macro_location(
if (n > 0 && (n & (n+1)) == 0)
{
hptr = htable[i];
MacroInfo **oldhptr = hptr;
hptr = (MacroInfo **)realloc(hptr, (2*(n+1))*sizeof(MacroInfo *));
if (!hptr)
{
free(oldhptr);
return NULL;
}
htable[i] = hptr;
hptr += n;
}
......@@ -1396,7 +1402,7 @@ static int preproc_evaluate_define(
{
if (tokens->tok != TOK_ID && tokens->tok != TOK_ELLIPSIS)
{
if (params) { free((char **)params); }
free((char **)params);
#if PREPROC_DEBUG
fprintf(stderr, "syntax error %d\n", __LINE__);
#endif
......@@ -1433,7 +1439,7 @@ static int preproc_evaluate_define(
}
else if (tokens->tok != ')')
{
if (params) { free((char **)params); }
free((char **)params);
#if PREPROC_DEBUG
fprintf(stderr, "syntax error %d\n", __LINE__);
#endif
......@@ -1451,11 +1457,11 @@ static int preproc_evaluate_define(
macro = *macro_p;
if (macro)
{
free((char **)params);
if (preproc_identical(macro->Definition, definition))
{
return VTK_PARSE_OK;
}
if (params) { free((char **)params); }
#if PREPROC_DEBUG
fprintf(stderr, "macro redefined %d\n", __LINE__);
#endif
......@@ -1555,7 +1561,13 @@ const char *preproc_find_include_file(
if (m+1 > outputsize)
{
outputsize += m+1;
char *oldoutput = output;
output = (char *)realloc(output, outputsize);
if (!output)
{
free(oldoutput);
return NULL;
}
}
strncpy(output, filename, m);
output[m] = '\0';
......@@ -1610,7 +1622,13 @@ const char *preproc_find_include_file(
if (m+j+1 > outputsize)
{
outputsize += m+j+1;
char *oldoutput = output;
output = (char *)realloc(output, outputsize);
if (!output)
{
free(oldoutput);
return NULL;
}
}
if (j > 0)
{
......@@ -1624,7 +1642,13 @@ const char *preproc_find_include_file(
if (m+1 > outputsize)
{
outputsize += m+1;
char *oldoutput = output;
output = (char *)realloc(output, outputsize);
if (!output)
{
free(oldoutput);
return NULL;
}
}
strncpy(output, filename, m);
output[m] = '\0';
......@@ -1638,7 +1662,13 @@ const char *preproc_find_include_file(
if (j + m + 2 > outputsize)
{
outputsize += j+m+2;
char *oldoutput = output;
output = (char *)realloc(output, outputsize);
if (!output)
{
free(oldoutput);
return NULL;
}
}
strncpy(output, directory, j);
......@@ -1716,7 +1746,17 @@ void preproc_escape_string(
while (j+4 > linelen)
{
linelen *= 2;
char *oldline = line;
line = (char *)realloc(line, linelen);
if (!line)
{
free(r);
free(oldline);
*linep = NULL;
*linelenp = -1;
*jp = 0; /* XXX: Is this right? */
return;
}
}
if ((r[i] >= ' ' && r[i] <= '~') || (r[i] & 0x80) != 0)
......@@ -1909,7 +1949,13 @@ static int preproc_include_file(
while (j+4 > linelen)
{
linelen *= 2;
char *oldline = line;
line = (char *)realloc(line, linelen);
if (!line)
{
free(oldline);