Skip to content

Commit 00f3e1a

Browse files
committed
Implement —fail-on-warnings command-line argument
1 parent bf32e1e commit 00f3e1a

File tree

5 files changed

+76
-21
lines changed

5 files changed

+76
-21
lines changed

docs/commandline.rst

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ Available options are:
9797

9898
Disable warnings.
9999

100+
.. option:: --fail-on-warnings
101+
102+
Treat warnings as errors. Has no effect if used with --no-warnings.
103+
100104
.. option:: -v --version
101105

102106
Show version information.

yara.c

+31-8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ typedef struct _MODULE_DATA
8282

8383
} MODULE_DATA;
8484

85+
8586
typedef struct _THREAD_ARGS
8687
{
8788
YR_RULES* rules;
@@ -90,13 +91,21 @@ typedef struct _THREAD_ARGS
9091
} THREAD_ARGS;
9192

9293

93-
typedef struct _QUEUED_FILE {
94-
94+
typedef struct _QUEUED_FILE
95+
{
9596
char* path;
9697

9798
} QUEUED_FILE;
9899

99100

101+
typedef struct COMPILER_RESULTS
102+
{
103+
int errors;
104+
int warnings;
105+
106+
} COMPILER_RESULTS;
107+
108+
100109
#define MAX_ARGS_TAG 32
101110
#define MAX_ARGS_IDENTIFIER 32
102111
#define MAX_ARGS_EXT_VAR 32
@@ -125,6 +134,7 @@ int limit = 0;
125134
int timeout = 1000000;
126135
int stack_size = DEFAULT_STACK_SIZE;
127136
int threads = 8;
137+
int fail_on_warnings = FALSE;
128138

129139

130140
#define USAGE_STRING \
@@ -184,6 +194,9 @@ args_option_t options[] =
184194
OPT_BOOLEAN('w', "no-warnings", &ignore_warnings,
185195
"disable warnings"),
186196

197+
OPT_BOOLEAN(0, "fail-on-warnings", &fail_on_warnings,
198+
"fail on warnings"),
199+
187200
OPT_BOOLEAN('v', "version", &show_version,
188201
"show version information"),
189202

@@ -526,10 +539,12 @@ void print_compiler_error(
526539
{
527540
fprintf(stderr, "%s(%d): error: %s\n", file_name, line_number, message);
528541
}
529-
else
542+
else if (!ignore_warnings)
530543
{
531-
if (!ignore_warnings)
532-
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
544+
COMPILER_RESULTS* compiler_results = (COMPILER_RESULTS*) user_data;
545+
compiler_results->warnings++;
546+
547+
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
533548
}
534549
}
535550

@@ -1091,7 +1106,12 @@ int main(
10911106
exit_with_code(EXIT_FAILURE);
10921107
}
10931108

1094-
yr_compiler_set_callback(compiler, print_compiler_error, NULL);
1109+
COMPILER_RESULTS cr = {
1110+
.errors = 0,
1111+
.warnings = 0
1112+
};
1113+
1114+
yr_compiler_set_callback(compiler, print_compiler_error, &cr);
10951115

10961116
FILE* rule_file = fopen(argv[0], "r");
10971117

@@ -1101,11 +1121,14 @@ int main(
11011121
exit_with_code(EXIT_FAILURE);
11021122
}
11031123

1104-
int errors = yr_compiler_add_file(compiler, rule_file, NULL, argv[0]);
1124+
cr.errors = yr_compiler_add_file(compiler, rule_file, NULL, argv[0]);
11051125

11061126
fclose(rule_file);
11071127

1108-
if (errors > 0)
1128+
if (cr.errors > 0)
1129+
exit_with_code(EXIT_FAILURE);
1130+
1131+
if (fail_on_warnings && cr.warnings > 0)
11091132
exit_with_code(EXIT_FAILURE);
11101133

11111134
result = yr_compiler_get_rules(compiler, &rules);

yara.man

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ Speeds up scanning by searching only for the first occurrence of each pattern.
7474
.B \-w " --no-warnings"
7575
Disable warnings.
7676
.TP
77+
.B " --fail-on-warnings"
78+
Treat warnings as errors. Has no effect if used with
79+
.B --no-warnings.
80+
.TP
7781
.B \-v " --version"
7882
Show version information.
7983
.SH EXAMPLES

yarac.c

+29-7
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5656
#define MAX_ARGS_EXT_VAR 32
5757

5858

59+
typedef struct COMPILER_RESULTS
60+
{
61+
int errors;
62+
int warnings;
63+
64+
} COMPILER_RESULTS;
65+
66+
5967
char* ext_vars[MAX_ARGS_EXT_VAR + 1];
6068
int ignore_warnings = FALSE;
6169
int show_version = FALSE;
6270
int show_help = FALSE;
71+
int fail_on_warnings = FALSE;
6372

6473

6574
#define USAGE_STRING \
@@ -73,6 +82,9 @@ args_option_t options[] =
7382
OPT_BOOLEAN('w', "no-warnings", &ignore_warnings,
7483
"disable warnings"),
7584

85+
OPT_BOOLEAN(0, "fail-on-warnings", &fail_on_warnings,
86+
"fail on warnings"),
87+
7688
OPT_BOOLEAN('v', "version", &show_version,
7789
"show version information"),
7890

@@ -108,10 +120,12 @@ void report_error(
108120
{
109121
fprintf(stderr, "%s(%d): error: %s\n", file_name, line_number, message);
110122
}
111-
else
123+
else if (!ignore_warnings)
112124
{
113-
if (!ignore_warnings)
114-
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
125+
COMPILER_RESULTS* compiler_results = (COMPILER_RESULTS*) user_data;
126+
compiler_results->warnings++;
127+
128+
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
115129
}
116130
}
117131

@@ -189,7 +203,7 @@ int main(
189203
{
190204
printf("%s\n\n", USAGE_STRING);
191205

192-
args_print_usage(options, 25);
206+
args_print_usage(options, 35);
193207
printf("\nSend bug reports and suggestions to: %s.\n", PACKAGE_BUGREPORT);
194208

195209
return EXIT_SUCCESS;
@@ -215,7 +229,12 @@ int main(
215229
if (!define_external_variables(compiler))
216230
exit_with_code(EXIT_FAILURE);
217231

218-
yr_compiler_set_callback(compiler, report_error, NULL);
232+
COMPILER_RESULTS cr = {
233+
.errors = 0,
234+
.warnings = 0
235+
};
236+
237+
yr_compiler_set_callback(compiler, report_error, &cr);
219238

220239
for (int i = 0; i < argc - 1; i++)
221240
{
@@ -239,12 +258,15 @@ int main(
239258

240259
if (rule_file != NULL)
241260
{
242-
int errors = yr_compiler_add_file(
261+
cr.errors = yr_compiler_add_file(
243262
compiler, rule_file, ns, file_name);
244263

245264
fclose(rule_file);
246265

247-
if (errors) // errors during compilation
266+
if (cr.errors) // errors during compilation
267+
exit_with_code(EXIT_FAILURE);
268+
269+
if (fail_on_warnings && cr.warnings > 0)
248270
exit_with_code(EXIT_FAILURE);
249271
}
250272
else

yarac.man

+8-6
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ if it’s a path to a directory all the files contained in it will be scanned.
2929
\fB-d\fP <identifier>=<value>
3030
define external variable.
3131
.TP
32-
.B
33-
\fB-w\fP
34-
disable warnings.
32+
.B \-w " --no-warnings"
33+
Disable warnings.
3534
.TP
36-
.B
37-
\fB-v\fP
38-
show version information.
35+
.B " --fail-on-warnings"
36+
Treat warnings as errors. Has no effect if used with
37+
.B --no-warnings.
38+
.TP
39+
.B \-v " --version"
40+
Show version information.
3941
.SH EXAMPLE
4042
The \fB-d\fP is used to define external variables. For example:
4143
.PP

0 commit comments

Comments
 (0)