Skip to content

Commit

Permalink
Added warning on unknown escape sequences (#1880)
Browse files Browse the repository at this point in the history
* Added warning on unknown escape sequences

* fixed memory leaks and different output when using regex as operand with strict-escape parameter

* Remove some unwanted characters from strict escaping, add testcases
  • Loading branch information
TommYDeeee authored Aug 23, 2023
1 parent e40b991 commit d7e67ff
Show file tree
Hide file tree
Showing 17 changed files with 625 additions and 297 deletions.
12 changes: 12 additions & 0 deletions cli/yara.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ static bool ignore_warnings = false;
static bool fast_scan = false;
static bool negate = false;
static bool print_count_only = false;
static bool strict_escape = false;
static bool fail_on_warnings = false;
static bool rules_are_compiled = false;
static bool disable_console_logs = false;
Expand Down Expand Up @@ -192,6 +193,12 @@ args_option_t options[] = {
&print_count_only,
_T("print only number of matches")),

OPT_BOOLEAN(
'E',
_T("strict-escape"),
&strict_escape,
_T("warn on unknown escape sequences")),

OPT_STRING_MULTI(
'd',
_T("define"),
Expand Down Expand Up @@ -1565,6 +1572,11 @@ int _tmain(int argc, const char_t** argv)

yr_compiler_set_callback(compiler, print_compiler_error, &cr);

if (strict_escape)
compiler->strict_escape = true;
else
compiler->strict_escape = false;

if (!compile_files(compiler, argc, argv))
exit_with_code(EXIT_FAILURE);

Expand Down
12 changes: 12 additions & 0 deletions cli/yarac.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static char* ext_vars[MAX_ARGS_EXT_VAR + 1];
static bool ignore_warnings = false;
static bool show_version = false;
static bool show_help = false;
static bool strict_escape = false;
static bool fail_on_warnings = false;
static long max_strings_per_rule = DEFAULT_MAX_STRINGS_PER_RULE;

Expand Down Expand Up @@ -103,6 +104,12 @@ args_option_t options[] = {

OPT_BOOLEAN('h', _T("help"), &show_help, _T("show this help and exit")),

OPT_BOOLEAN(
'E',
_T("strict-escape"),
&strict_escape,
_T("warn on unknown escape sequences")),

OPT_LONG(
0,
_T("max-strings-per-rule"),
Expand Down Expand Up @@ -233,6 +240,11 @@ int _tmain(int argc, const char_t** argv)

yr_compiler_set_callback(compiler, report_error, &cr);

if (strict_escape)
compiler->strict_escape = true;
else
compiler->strict_escape = false;

if (!compile_files(compiler, argc, argv))
exit_with_code(EXIT_FAILURE);

Expand Down
2 changes: 1 addition & 1 deletion libyara/base64.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ int _yr_base64_create_regexp(
// printf("%s\n", re_str);

FAIL_ON_ERROR_WITH_CLEANUP(
yr_re_parse(re_str, re_ast, re_error), yr_free(re_str));
yr_re_parse(re_str, re_ast, re_error, RE_PARSER_FLAG_NONE), yr_free(re_str));

yr_free(re_str);

Expand Down
1 change: 1 addition & 0 deletions libyara/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ YR_API int yr_compiler_create(YR_COMPILER** compiler)
new_compiler->re_ast_clbk_user_data = NULL;
new_compiler->last_error = ERROR_SUCCESS;
new_compiler->last_error_line = 0;
new_compiler->strict_escape = false;
new_compiler->current_line = 0;
new_compiler->file_name_stack_ptr = 0;
new_compiler->fixup_stack_head = NULL;
Expand Down
Loading

0 comments on commit d7e67ff

Please sign in to comment.