Commit 529dccb1 authored by Ben Boeckel's avatar Ben Boeckel

vtkParse: address cppcheck messages

Mainly improper realloc usage, but also a few memory leaks.

Change-Id: I83f965ee3457ac4dd05ee2475f1d529206c71478
parent ae3fa6e9
......@@ -4524,6 +4524,7 @@ const char *get_macro_arguments()
if (c1 != '(')
{
unput(c1);
free(cp);
return NULL;
}
......@@ -4577,7 +4578,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)
......@@ -4586,7 +4593,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 == '\'')
......@@ -4616,7 +4629,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);
......@@ -4626,7 +4645,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 == '(')
......@@ -4849,7 +4874,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 */
......@@ -4998,6 +5029,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;
}
......@@ -5084,6 +5120,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;
}
......@@ -5137,8 +5178,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();
......@@ -5210,6 +5261,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)
......@@ -5292,6 +5348,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 */
......
......@@ -604,6 +604,7 @@ const char *get_macro_arguments()
if (c1 != '(')
{
unput(c1);
free(cp);
return NULL;
}
......@@ -657,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)
......@@ -666,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 == '\'')
......@@ -696,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);
......@@ -706,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 == '(')
......@@ -929,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 */
......@@ -1078,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;
}
......@@ -1164,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;
}
......@@ -1217,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();
......@@ -1290,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)
......@@ -1372,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.
......@@ -417,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)
......
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