--- a/pscan.c +++ b/pscan.c @@ -29,8 +29,8 @@ #include "pscan.h" extern int yylex(); -extern int yylineno; extern FILE *yyout, *yyin; +int cur_lineno=1; /* * This function does nothing useful. @@ -98,7 +98,7 @@ static void usage(void) { fprintf(stderr, "Usage: pscan [-vw] [-p problem_file] \n"); fprintf(stderr, "Attempts to discover a number of common security abuses in C source files.\n\n"); - fprintf(stderr, " -v Verbose mode. Can be use multiple times for more output.\n"); + fprintf(stderr, " -v Verbose mode. \n"); fprintf(stderr, " -w Show warnings when a variable is used as the format argument.\n"); fprintf(stderr, " -p file Read additional problem definitions from .\n"); exit(1); @@ -204,7 +204,7 @@ int main(int argc,char **argv) break; case 'v': - verbose++; + verbose= TRUE; break; case 'w': @@ -259,7 +259,7 @@ int main(int argc,char **argv) /* * Initialize our variables. */ - yylineno = 1; + cur_lineno = 1; if (verbose) { printf("Scanning %s ...\n", filename); @@ -278,18 +278,22 @@ int main(int argc,char **argv) /* * And finally, print out a summary of the total problems. */ - if (total_errors != 0) { - if (verbose) { - if (warnings) { - printf("Warnings: %d\n", total_warnings); - } + if (verbose) { + if (total_errors != 0 ) { printf("Total problems identified: %d\n", total_errors); + } + if (warnings) { + if (total_warnings != 0 ) { + printf("Warnings: %d\n",total_warnings); + } } - exit(1); } - - exit(0); + + if (total_errors!=0) + exit(1); + else + exit(0); } /* @@ -298,8 +302,9 @@ int main(int argc,char **argv) */ void check_function(parser_state_t *state) { - assert(state != NULL); + int erroneous=0; + assert(state != NULL); /* * This was a reference to a function WITHOUT an opening brace, * so it's not a function call. Ignore it. @@ -308,7 +313,6 @@ void check_function(parser_state_t *stat return; } - if (verbose == 0) { /* * The problem function has the SAME number of arguments as the * placement of the format argument. i.e. The LAST argument of the @@ -327,9 +331,11 @@ void check_function(parser_state_t *stat state->line, state->problem->function, state->problem->fmt_arg); - total_errors++; + erroneous=1; + + } - } else if (warnings && + if (warnings && (state->constant_string != state->problem->fmt_arg)) { printf("%s:%d Warning: %s uses non-constant string for format argument %d.\n", filename, @@ -339,25 +345,28 @@ void check_function(parser_state_t *stat total_warnings++; } - } else { + if (verbose) { /* - * verbose = 1, print out more stuff. + * verbose =TRUE, print out more stuff. */ printf("%s:%d FUNC %s ", filename, state->line, state->problem->function); if (state->problem->fmt_arg == state->args) { printf("Last argument is "); - if (state->constant_string) { + if (state->constant_string==state->args) { printf("constant string: OK\n"); } else { printf("variable or reference: BAD\n"); - total_errors++; + erroneous=1; } } else { printf("format string with %d parameters: OK\n", state->args - state->problem->fmt_arg); } } + + if (erroneous) total_errors++; + } /* @@ -399,7 +408,7 @@ parser_state_t *setup_checks(const char } state->problem = problem; - state->line = yylineno; + state->line = cur_lineno; state->braces = 0; state->args = -1; state->constant_string = -1; @@ -423,7 +432,7 @@ parser_state_t *setup_checks(const char } state->problem = problem; - state->line = yylineno; + state->line = cur_lineno; state->braces = 0; state->args = -1; state->constant_string = -1; --- a/pscan.h +++ b/pscan.h @@ -29,6 +29,8 @@ #define PROBLEMATIC TRUE #define NOT_PROBLEMATIC FALSE +#define YY_NO_UNPUT + /* * The maximum number of user-defined problem functions which may be * read from a .pscan problem definition file. --- a/test.c +++ b/test.c @@ -147,3 +147,9 @@ fprintf[1] = 1; /* OK */ * NetBSD allows err(1,NULL). We should, too. */ err(1, NULL); + +/* + * A reserved function sizeof() as argument should not affect result when testing snprintf + * expected that this is an error + */ +snprintf(b,sizeof(b),argv[1]);