| 1 |
--- a/pscan.c |
| 2 |
+++ b/pscan.c |
| 3 |
@@ -29,8 +29,8 @@ |
| 4 |
#include "pscan.h" |
| 5 |
|
| 6 |
extern int yylex(); |
| 7 |
-extern int yylineno; |
| 8 |
extern FILE *yyout, *yyin; |
| 9 |
+int cur_lineno=1; |
| 10 |
|
| 11 |
/* |
| 12 |
* This function does nothing useful. |
| 13 |
@@ -98,7 +98,7 @@ static void usage(void) |
| 14 |
{ |
| 15 |
fprintf(stderr, "Usage: pscan [-vw] [-p problem_file] <files ...>\n"); |
| 16 |
fprintf(stderr, "Attempts to discover a number of common security abuses in C source files.\n\n"); |
| 17 |
- fprintf(stderr, " -v Verbose mode. Can be use multiple times for more output.\n"); |
| 18 |
+ fprintf(stderr, " -v Verbose mode. \n"); |
| 19 |
fprintf(stderr, " -w Show warnings when a variable is used as the format argument.\n"); |
| 20 |
fprintf(stderr, " -p file Read additional problem <function,offset> definitions from <file>.\n"); |
| 21 |
exit(1); |
| 22 |
@@ -204,7 +204,7 @@ int main(int argc,char **argv) |
| 23 |
break; |
| 24 |
|
| 25 |
case 'v': |
| 26 |
- verbose++; |
| 27 |
+ verbose= TRUE; |
| 28 |
break; |
| 29 |
|
| 30 |
case 'w': |
| 31 |
@@ -259,7 +259,7 @@ int main(int argc,char **argv) |
| 32 |
/* |
| 33 |
* Initialize our variables. |
| 34 |
*/ |
| 35 |
- yylineno = 1; |
| 36 |
+ cur_lineno = 1; |
| 37 |
|
| 38 |
if (verbose) { |
| 39 |
printf("Scanning %s ...\n", filename); |
| 40 |
@@ -278,18 +278,22 @@ int main(int argc,char **argv) |
| 41 |
/* |
| 42 |
* And finally, print out a summary of the total problems. |
| 43 |
*/ |
| 44 |
- if (total_errors != 0) { |
| 45 |
- if (verbose) { |
| 46 |
- if (warnings) { |
| 47 |
- printf("Warnings: %d\n", total_warnings); |
| 48 |
- } |
| 49 |
+ if (verbose) { |
| 50 |
+ if (total_errors != 0 ) { |
| 51 |
printf("Total problems identified: %d\n", total_errors); |
| 52 |
+ } |
| 53 |
|
| 54 |
+ if (warnings) { |
| 55 |
+ if (total_warnings != 0 ) { |
| 56 |
+ printf("Warnings: %d\n",total_warnings); |
| 57 |
+ } |
| 58 |
} |
| 59 |
- exit(1); |
| 60 |
} |
| 61 |
- |
| 62 |
- exit(0); |
| 63 |
+ |
| 64 |
+ if (total_errors!=0) |
| 65 |
+ exit(1); |
| 66 |
+ else |
| 67 |
+ exit(0); |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
@@ -298,8 +302,9 @@ int main(int argc,char **argv) |
| 72 |
*/ |
| 73 |
void check_function(parser_state_t *state) |
| 74 |
{ |
| 75 |
- assert(state != NULL); |
| 76 |
+ int erroneous=0; |
| 77 |
|
| 78 |
+ assert(state != NULL); |
| 79 |
/* |
| 80 |
* This was a reference to a function WITHOUT an opening brace, |
| 81 |
* so it's not a function call. Ignore it. |
| 82 |
@@ -308,7 +313,6 @@ void check_function(parser_state_t *stat |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
- if (verbose == 0) { |
| 87 |
/* |
| 88 |
* The problem function has the SAME number of arguments as the |
| 89 |
* placement of the format argument. i.e. The LAST argument of the |
| 90 |
@@ -327,9 +331,11 @@ void check_function(parser_state_t *stat |
| 91 |
state->line, |
| 92 |
state->problem->function, |
| 93 |
state->problem->fmt_arg); |
| 94 |
- total_errors++; |
| 95 |
+ erroneous=1; |
| 96 |
+ |
| 97 |
+ } |
| 98 |
|
| 99 |
- } else if (warnings && |
| 100 |
+ if (warnings && |
| 101 |
(state->constant_string != state->problem->fmt_arg)) { |
| 102 |
printf("%s:%d Warning: %s uses non-constant string for format argument %d.\n", |
| 103 |
filename, |
| 104 |
@@ -339,25 +345,28 @@ void check_function(parser_state_t *stat |
| 105 |
total_warnings++; |
| 106 |
} |
| 107 |
|
| 108 |
- } else { |
| 109 |
+ if (verbose) { |
| 110 |
/* |
| 111 |
- * verbose = 1, print out more stuff. |
| 112 |
+ * verbose =TRUE, print out more stuff. |
| 113 |
*/ |
| 114 |
printf("%s:%d FUNC %s ", filename, state->line, |
| 115 |
state->problem->function); |
| 116 |
if (state->problem->fmt_arg == state->args) { |
| 117 |
printf("Last argument is "); |
| 118 |
- if (state->constant_string) { |
| 119 |
+ if (state->constant_string==state->args) { |
| 120 |
printf("constant string: OK\n"); |
| 121 |
} else { |
| 122 |
printf("variable or reference: BAD\n"); |
| 123 |
- total_errors++; |
| 124 |
+ erroneous=1; |
| 125 |
} |
| 126 |
} else { |
| 127 |
printf("format string with %d parameters: OK\n", |
| 128 |
state->args - state->problem->fmt_arg); |
| 129 |
} |
| 130 |
} |
| 131 |
+ |
| 132 |
+ if (erroneous) total_errors++; |
| 133 |
+ |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
@@ -399,7 +408,7 @@ parser_state_t *setup_checks(const char |
| 138 |
} |
| 139 |
|
| 140 |
state->problem = problem; |
| 141 |
- state->line = yylineno; |
| 142 |
+ state->line = cur_lineno; |
| 143 |
state->braces = 0; |
| 144 |
state->args = -1; |
| 145 |
state->constant_string = -1; |
| 146 |
@@ -423,7 +432,7 @@ parser_state_t *setup_checks(const char |
| 147 |
} |
| 148 |
|
| 149 |
state->problem = problem; |
| 150 |
- state->line = yylineno; |
| 151 |
+ state->line = cur_lineno; |
| 152 |
state->braces = 0; |
| 153 |
state->args = -1; |
| 154 |
state->constant_string = -1; |
| 155 |
--- a/pscan.h |
| 156 |
+++ b/pscan.h |
| 157 |
@@ -29,6 +29,8 @@ |
| 158 |
#define PROBLEMATIC TRUE |
| 159 |
#define NOT_PROBLEMATIC FALSE |
| 160 |
|
| 161 |
+#define YY_NO_UNPUT |
| 162 |
+ |
| 163 |
/* |
| 164 |
* The maximum number of user-defined problem functions which may be |
| 165 |
* read from a .pscan problem definition file. |
| 166 |
--- a/test.c |
| 167 |
+++ b/test.c |
| 168 |
@@ -147,3 +147,9 @@ fprintf[1] = 1; /* OK */ |
| 169 |
* NetBSD allows err(1,NULL). We should, too. |
| 170 |
*/ |
| 171 |
err(1, NULL); |
| 172 |
+ |
| 173 |
+/* |
| 174 |
+ * A reserved function sizeof() as argument should not affect result when testing snprintf |
| 175 |
+ * expected that this is an error |
| 176 |
+ */ |
| 177 |
+snprintf(b,sizeof(b),argv[1]); |