Skip to content

Commit

Permalink
Implement —fail-on-warnings command-line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Nov 22, 2016
1 parent bf32e1e commit 00f3e1a
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
4 changes: 4 additions & 0 deletions docs/commandline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Available options are:

Disable warnings.

.. option:: --fail-on-warnings

Treat warnings as errors. Has no effect if used with --no-warnings.

.. option:: -v --version

Show version information.
Expand Down
39 changes: 31 additions & 8 deletions yara.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef struct _MODULE_DATA

} MODULE_DATA;


typedef struct _THREAD_ARGS
{
YR_RULES* rules;
Expand All @@ -90,13 +91,21 @@ typedef struct _THREAD_ARGS
} THREAD_ARGS;


typedef struct _QUEUED_FILE {

typedef struct _QUEUED_FILE
{
char* path;

} QUEUED_FILE;


typedef struct COMPILER_RESULTS
{
int errors;
int warnings;

} COMPILER_RESULTS;


#define MAX_ARGS_TAG 32
#define MAX_ARGS_IDENTIFIER 32
#define MAX_ARGS_EXT_VAR 32
Expand Down Expand Up @@ -125,6 +134,7 @@ int limit = 0;
int timeout = 1000000;
int stack_size = DEFAULT_STACK_SIZE;
int threads = 8;
int fail_on_warnings = FALSE;


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

OPT_BOOLEAN(0, "fail-on-warnings", &fail_on_warnings,
"fail on warnings"),

OPT_BOOLEAN('v', "version", &show_version,
"show version information"),

Expand Down Expand Up @@ -526,10 +539,12 @@ void print_compiler_error(
{
fprintf(stderr, "%s(%d): error: %s\n", file_name, line_number, message);
}
else
else if (!ignore_warnings)
{
if (!ignore_warnings)
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
COMPILER_RESULTS* compiler_results = (COMPILER_RESULTS*) user_data;
compiler_results->warnings++;

fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
}
}

Expand Down Expand Up @@ -1091,7 +1106,12 @@ int main(
exit_with_code(EXIT_FAILURE);
}

yr_compiler_set_callback(compiler, print_compiler_error, NULL);
COMPILER_RESULTS cr = {
.errors = 0,
.warnings = 0
};

yr_compiler_set_callback(compiler, print_compiler_error, &cr);

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

Expand All @@ -1101,11 +1121,14 @@ int main(
exit_with_code(EXIT_FAILURE);
}

int errors = yr_compiler_add_file(compiler, rule_file, NULL, argv[0]);
cr.errors = yr_compiler_add_file(compiler, rule_file, NULL, argv[0]);

fclose(rule_file);

if (errors > 0)
if (cr.errors > 0)
exit_with_code(EXIT_FAILURE);

if (fail_on_warnings && cr.warnings > 0)
exit_with_code(EXIT_FAILURE);

result = yr_compiler_get_rules(compiler, &rules);
Expand Down
4 changes: 4 additions & 0 deletions yara.man
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ Speeds up scanning by searching only for the first occurrence of each pattern.
.B \-w " --no-warnings"
Disable warnings.
.TP
.B " --fail-on-warnings"
Treat warnings as errors. Has no effect if used with
.B --no-warnings.
.TP
.B \-v " --version"
Show version information.
.SH EXAMPLES
Expand Down
36 changes: 29 additions & 7 deletions yarac.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define MAX_ARGS_EXT_VAR 32


typedef struct COMPILER_RESULTS
{
int errors;
int warnings;

} COMPILER_RESULTS;


char* ext_vars[MAX_ARGS_EXT_VAR + 1];
int ignore_warnings = FALSE;
int show_version = FALSE;
int show_help = FALSE;
int fail_on_warnings = FALSE;


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

OPT_BOOLEAN(0, "fail-on-warnings", &fail_on_warnings,
"fail on warnings"),

OPT_BOOLEAN('v', "version", &show_version,
"show version information"),

Expand Down Expand Up @@ -108,10 +120,12 @@ void report_error(
{
fprintf(stderr, "%s(%d): error: %s\n", file_name, line_number, message);
}
else
else if (!ignore_warnings)
{
if (!ignore_warnings)
fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
COMPILER_RESULTS* compiler_results = (COMPILER_RESULTS*) user_data;
compiler_results->warnings++;

fprintf(stderr, "%s(%d): warning: %s\n", file_name, line_number, message);
}
}

Expand Down Expand Up @@ -189,7 +203,7 @@ int main(
{
printf("%s\n\n", USAGE_STRING);

args_print_usage(options, 25);
args_print_usage(options, 35);
printf("\nSend bug reports and suggestions to: %s.\n", PACKAGE_BUGREPORT);

return EXIT_SUCCESS;
Expand All @@ -215,7 +229,12 @@ int main(
if (!define_external_variables(compiler))
exit_with_code(EXIT_FAILURE);

yr_compiler_set_callback(compiler, report_error, NULL);
COMPILER_RESULTS cr = {
.errors = 0,
.warnings = 0
};

yr_compiler_set_callback(compiler, report_error, &cr);

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

if (rule_file != NULL)
{
int errors = yr_compiler_add_file(
cr.errors = yr_compiler_add_file(
compiler, rule_file, ns, file_name);

fclose(rule_file);

if (errors) // errors during compilation
if (cr.errors) // errors during compilation
exit_with_code(EXIT_FAILURE);

if (fail_on_warnings && cr.warnings > 0)
exit_with_code(EXIT_FAILURE);
}
else
Expand Down
14 changes: 8 additions & 6 deletions yarac.man
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ if it’s a path to a directory all the files contained in it will be scanned.
\fB-d\fP <identifier>=<value>
define external variable.
.TP
.B
\fB-w\fP
disable warnings.
.B \-w " --no-warnings"
Disable warnings.
.TP
.B
\fB-v\fP
show version information.
.B " --fail-on-warnings"
Treat warnings as errors. Has no effect if used with
.B --no-warnings.
.TP
.B \-v " --version"
Show version information.
.SH EXAMPLE
The \fB-d\fP is used to define external variables. For example:
.PP
Expand Down

0 comments on commit 00f3e1a

Please sign in to comment.