[Forensics-changes] [yara] 386/407: Improve error handling in low memory conditions
Hilko Bengen
bengen at moszumanska.debian.org
Sat Jul 1 10:28:47 UTC 2017
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to annotated tag v3.3.0
in repository yara.
commit 2c1c67cc9148301d68d50a454fcd9ddc405f0a00
Author: Victor M. Alvarez <plusvic at gmail.com>
Date: Fri Feb 6 20:11:42 2015 +0100
Improve error handling in low memory conditions
---
libyara/ahocorasick.c | 8 +-
libyara/arena.c | 3 +
libyara/atoms.c | 3 +
libyara/compiler.c | 81 ++++++++---------
libyara/hash.c | 5 +-
libyara/include/yara/ahocorasick.h | 2 +-
libyara/include/yara/object.h | 6 +-
libyara/lexer.c | 178 ++++++++++++++++++-------------------
libyara/lexer.l | 34 +++----
libyara/modules.c | 26 +++---
libyara/object.c | 77 ++++++++++------
11 files changed, 226 insertions(+), 197 deletions(-)
diff --git a/libyara/ahocorasick.c b/libyara/ahocorasick.c
index 662f5f6..ee77361 100644
--- a/libyara/ahocorasick.c
+++ b/libyara/ahocorasick.c
@@ -357,7 +357,7 @@ YR_AC_STATE* _yr_ac_create_state(
// be called after all the strings have been added to the automaton.
//
-void yr_ac_create_failure_links(
+int yr_ac_create_failure_links(
YR_ARENA* arena,
YR_AC_AUTOMATON* automaton)
{
@@ -387,7 +387,7 @@ void yr_ac_create_failure_links(
while (state != NULL)
{
- _yr_ac_queue_push(&queue, state);
+ FAIL_ON_ERROR(_yr_ac_queue_push(&queue, state));
state->failure = root_state;
state = _yr_ac_next_transition(root_state, &transition);
}
@@ -420,7 +420,7 @@ void yr_ac_create_failure_links(
while (transition_state != NULL)
{
- _yr_ac_queue_push(&queue, transition_state);
+ FAIL_ON_ERROR(_yr_ac_queue_push(&queue, transition_state));
failure_state = current_state->failure;
while (1)
@@ -469,6 +469,8 @@ void yr_ac_create_failure_links(
}
} // while(!__yr_ac_queue_is_empty(&queue))
+
+ return ERROR_SUCCESS;
}
diff --git a/libyara/arena.c b/libyara/arena.c
index 2e866ef..8111683 100644
--- a/libyara/arena.c
+++ b/libyara/arena.c
@@ -266,6 +266,9 @@ void yr_arena_destroy(
YR_ARENA_PAGE* page;
YR_ARENA_PAGE* next_page;
+ if (arena == NULL)
+ return;
+
page = arena->page_list_head;
while(page != NULL)
diff --git a/libyara/atoms.c b/libyara/atoms.c
index 5b06708..8317699 100644
--- a/libyara/atoms.c
+++ b/libyara/atoms.c
@@ -1019,6 +1019,9 @@ int yr_atoms_extract_from_re(
atom_tree->root_node = _yr_atoms_extract_from_re_node(
re->root_node, atom_tree, atom_tree->root_node);
+ if (atom_tree->root_node == NULL)
+ return ERROR_INSUFICIENT_MEMORY;
+
if (atom_tree->current_leaf != NULL)
_yr_atoms_tree_node_append(atom_tree->root_node, atom_tree->current_leaf);
diff --git a/libyara/compiler.c b/libyara/compiler.c
index 5cbe92b..fd7fab1 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -35,7 +35,7 @@ YR_API int yr_compiler_create(
int result;
YR_COMPILER* new_compiler;
- new_compiler = (YR_COMPILER*) yr_malloc(sizeof(YR_COMPILER));
+ new_compiler = (YR_COMPILER*) yr_calloc(1, sizeof(YR_COMPILER));
if (new_compiler == NULL)
return ERROR_INSUFICIENT_MEMORY;
@@ -71,7 +71,7 @@ YR_API int yr_compiler_create(
result = yr_arena_create(65536, 0, &new_compiler->strings_arena);
if (result == ERROR_SUCCESS)
- result = yr_arena_create(65536, 0, &new_compiler->code_arena);
+ result = yr_arena_create(65536, 0, &new_compiler->code_arena);
if (result == ERROR_SUCCESS)
result = yr_arena_create(65536, 0, &new_compiler->re_code_arena);
@@ -109,37 +109,16 @@ YR_API int yr_compiler_create(
YR_API void yr_compiler_destroy(
YR_COMPILER* compiler)
{
- int i;
-
- if (compiler->compiled_rules_arena != NULL)
- yr_arena_destroy(compiler->compiled_rules_arena);
-
- if (compiler->sz_arena != NULL)
- yr_arena_destroy(compiler->sz_arena);
-
- if (compiler->rules_arena != NULL)
- yr_arena_destroy(compiler->rules_arena);
-
- if (compiler->strings_arena != NULL)
- yr_arena_destroy(compiler->strings_arena);
-
- if (compiler->code_arena != NULL)
- yr_arena_destroy(compiler->code_arena);
-
- if (compiler->re_code_arena != NULL)
- yr_arena_destroy(compiler->re_code_arena);
-
- if (compiler->automaton_arena != NULL)
- yr_arena_destroy(compiler->automaton_arena);
-
- if (compiler->externals_arena != NULL)
- yr_arena_destroy(compiler->externals_arena);
-
- if (compiler->namespaces_arena != NULL)
- yr_arena_destroy(compiler->namespaces_arena);
-
- if (compiler->metas_arena != NULL)
- yr_arena_destroy(compiler->metas_arena);
+ yr_arena_destroy(compiler->compiled_rules_arena);
+ yr_arena_destroy(compiler->sz_arena);
+ yr_arena_destroy(compiler->rules_arena);
+ yr_arena_destroy(compiler->strings_arena);
+ yr_arena_destroy(compiler->code_arena);
+ yr_arena_destroy(compiler->re_code_arena);
+ yr_arena_destroy(compiler->automaton_arena);
+ yr_arena_destroy(compiler->externals_arena);
+ yr_arena_destroy(compiler->namespaces_arena);
+ yr_arena_destroy(compiler->metas_arena);
yr_hash_table_destroy(
compiler->rules_table,
@@ -149,7 +128,7 @@ YR_API void yr_compiler_destroy(
compiler->objects_table,
(YR_HASH_TABLE_FREE_VALUE_FUNC) yr_object_destroy);
- for (i = 0; i < compiler->file_name_stack_ptr; i++)
+ for (int i = 0; i < compiler->file_name_stack_ptr; i++)
yr_free(compiler->file_name_stack[i]);
yr_free(compiler);
@@ -333,11 +312,20 @@ YR_API int yr_compiler_add_file(
_yr_compiler_push_file_name(compiler, file_name);
if (namespace_ != NULL)
- _yr_compiler_set_namespace(compiler, namespace_);
+ compiler->last_result = _yr_compiler_set_namespace(compiler, namespace_);
else
- _yr_compiler_set_namespace(compiler, "default");
+ compiler->last_result = _yr_compiler_set_namespace(compiler, "default");
+
+ if (compiler->last_result == ERROR_SUCCESS)
+ {
+ return yr_lex_parse_rules_file(rules_file, compiler);
+ }
+ else
+ {
+ compiler->errors++;
+ return compiler->errors;
+ }
- return yr_lex_parse_rules_file(rules_file, compiler);
}
@@ -352,11 +340,19 @@ YR_API int yr_compiler_add_string(
assert(compiler->compiled_rules_arena == NULL);
if (namespace_ != NULL)
- _yr_compiler_set_namespace(compiler, namespace_);
+ compiler->last_result = _yr_compiler_set_namespace(compiler, namespace_);
else
- _yr_compiler_set_namespace(compiler, "default");
+ compiler->last_result = _yr_compiler_set_namespace(compiler, "default");
- return yr_lex_parse_rules_string(rules_string, compiler);
+ if (compiler->last_result == ERROR_SUCCESS)
+ {
+ return yr_lex_parse_rules_string(rules_string, compiler);
+ }
+ else
+ {
+ compiler->errors++;
+ return compiler->errors;
+ }
}
int _yr_compiler_compile_rules(
@@ -398,11 +394,12 @@ int _yr_compiler_compile_rules(
NULL);
// Create Aho-Corasick automaton's failure links.
- yr_ac_create_failure_links(
+ result = yr_ac_create_failure_links(
compiler->automaton_arena,
compiler->automaton);
- result = yr_arena_create(1024, 0, &arena);
+ if (result == ERROR_SUCCESS)
+ result = yr_arena_create(1024, 0, &arena);
if (result == ERROR_SUCCESS)
result = yr_arena_allocate_struct(
diff --git a/libyara/hash.c b/libyara/hash.c
index dce5940..f63a65d 100644
--- a/libyara/hash.c
+++ b/libyara/hash.c
@@ -116,9 +116,10 @@ void yr_hash_table_destroy(
YR_HASH_TABLE_ENTRY* entry;
YR_HASH_TABLE_ENTRY* next_entry;
- int i;
+ if (table == NULL)
+ return;
- for (i = 0; i < table->size; i++)
+ for (int i = 0; i < table->size; i++)
{
entry = table->buckets[i];
diff --git a/libyara/include/yara/ahocorasick.h b/libyara/include/yara/ahocorasick.h
index 173253d..f8264fa 100644
--- a/libyara/include/yara/ahocorasick.h
+++ b/libyara/include/yara/ahocorasick.h
@@ -39,7 +39,7 @@ YR_AC_STATE* yr_ac_next_state(
uint8_t input);
-void yr_ac_create_failure_links(
+int yr_ac_create_failure_links(
YR_ARENA* arena,
YR_AC_AUTOMATON* automaton);
diff --git a/libyara/include/yara/object.h b/libyara/include/yara/object.h
index aa429cf..8602656 100644
--- a/libyara/include/yara/object.h
+++ b/libyara/include/yara/object.h
@@ -93,21 +93,21 @@ SIZED_STRING* yr_object_get_string(
...);
-void yr_object_set_integer(
+int yr_object_set_integer(
int64_t value,
YR_OBJECT* object,
const char* field,
...);
-void yr_object_set_float(
+int yr_object_set_float(
double value,
YR_OBJECT* object,
const char* field,
...);
-void yr_object_set_string(
+int yr_object_set_string(
const char* value,
size_t len,
YR_OBJECT* object,
diff --git a/libyara/lexer.c b/libyara/lexer.c
index 2c0d15b..4c0a72b 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -674,6 +674,20 @@ limitations under the License.
} \
}
+#define ALLOC_SIZED_STRING(str, str_len) \
+ SIZED_STRING* str = (SIZED_STRING*) yr_malloc( \
+ str_len + sizeof(SIZED_STRING)); \
+ if (str == NULL) \
+ { \
+ yyerror(yyscanner, compiler, "not enough memory"); \
+ yyterminate(); \
+ } \
+ else \
+ { \
+ str->length = (str_len); \
+ str->flags = 0; \
+ } \
+
#ifdef _WIN32
#define snprintf _snprintf
#endif
@@ -684,7 +698,7 @@ limitations under the License.
-#line 688 "lexer.c"
+#line 702 "lexer.c"
#define INITIAL 0
#define str 1
@@ -919,10 +933,10 @@ YY_DECL
register int yy_act;
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-#line 83 "lexer.l"
+#line 97 "lexer.l"
-#line 926 "lexer.c"
+#line 940 "lexer.c"
yylval = yylval_param;
@@ -1017,203 +1031,203 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
-#line 85 "lexer.l"
+#line 99 "lexer.l"
{ return _LT_; }
YY_BREAK
case 2:
YY_RULE_SETUP
-#line 86 "lexer.l"
+#line 100 "lexer.l"
{ return _GT_; }
YY_BREAK
case 3:
YY_RULE_SETUP
-#line 87 "lexer.l"
+#line 101 "lexer.l"
{ return _LE_; }
YY_BREAK
case 4:
YY_RULE_SETUP
-#line 88 "lexer.l"
+#line 102 "lexer.l"
{ return _GE_; }
YY_BREAK
case 5:
YY_RULE_SETUP
-#line 89 "lexer.l"
+#line 103 "lexer.l"
{ return _EQ_; }
YY_BREAK
case 6:
YY_RULE_SETUP
-#line 90 "lexer.l"
+#line 104 "lexer.l"
{ return _NEQ_; }
YY_BREAK
case 7:
YY_RULE_SETUP
-#line 91 "lexer.l"
+#line 105 "lexer.l"
{ return _SHIFT_LEFT_; }
YY_BREAK
case 8:
YY_RULE_SETUP
-#line 92 "lexer.l"
+#line 106 "lexer.l"
{ return _SHIFT_RIGHT_; }
YY_BREAK
case 9:
YY_RULE_SETUP
-#line 93 "lexer.l"
+#line 107 "lexer.l"
{ return _PRIVATE_; }
YY_BREAK
case 10:
YY_RULE_SETUP
-#line 94 "lexer.l"
+#line 108 "lexer.l"
{ return _GLOBAL_; }
YY_BREAK
case 11:
YY_RULE_SETUP
-#line 95 "lexer.l"
+#line 109 "lexer.l"
{ return _RULE_; }
YY_BREAK
case 12:
YY_RULE_SETUP
-#line 96 "lexer.l"
+#line 110 "lexer.l"
{ return _META_; }
YY_BREAK
case 13:
YY_RULE_SETUP
-#line 97 "lexer.l"
+#line 111 "lexer.l"
{ return _STRINGS_; }
YY_BREAK
case 14:
YY_RULE_SETUP
-#line 98 "lexer.l"
+#line 112 "lexer.l"
{ return _ASCII_; }
YY_BREAK
case 15:
YY_RULE_SETUP
-#line 99 "lexer.l"
+#line 113 "lexer.l"
{ return _WIDE_; }
YY_BREAK
case 16:
YY_RULE_SETUP
-#line 100 "lexer.l"
+#line 114 "lexer.l"
{ return _FULLWORD_; }
YY_BREAK
case 17:
YY_RULE_SETUP
-#line 101 "lexer.l"
+#line 115 "lexer.l"
{ return _NOCASE_; }
YY_BREAK
case 18:
YY_RULE_SETUP
-#line 102 "lexer.l"
+#line 116 "lexer.l"
{ return _CONDITION_; }
YY_BREAK
case 19:
YY_RULE_SETUP
-#line 103 "lexer.l"
+#line 117 "lexer.l"
{ return _TRUE_; }
YY_BREAK
case 20:
YY_RULE_SETUP
-#line 104 "lexer.l"
+#line 118 "lexer.l"
{ return _FALSE_; }
YY_BREAK
case 21:
YY_RULE_SETUP
-#line 105 "lexer.l"
+#line 119 "lexer.l"
{ return _NOT_; }
YY_BREAK
case 22:
YY_RULE_SETUP
-#line 106 "lexer.l"
+#line 120 "lexer.l"
{ return _AND_; }
YY_BREAK
case 23:
YY_RULE_SETUP
-#line 107 "lexer.l"
+#line 121 "lexer.l"
{ return _OR_; }
YY_BREAK
case 24:
YY_RULE_SETUP
-#line 108 "lexer.l"
+#line 122 "lexer.l"
{ return _AT_; }
YY_BREAK
case 25:
YY_RULE_SETUP
-#line 109 "lexer.l"
+#line 123 "lexer.l"
{ return _IN_; }
YY_BREAK
case 26:
YY_RULE_SETUP
-#line 110 "lexer.l"
+#line 124 "lexer.l"
{ return _OF_; }
YY_BREAK
case 27:
YY_RULE_SETUP
-#line 111 "lexer.l"
+#line 125 "lexer.l"
{ return _THEM_; }
YY_BREAK
case 28:
YY_RULE_SETUP
-#line 112 "lexer.l"
+#line 126 "lexer.l"
{ return _FOR_; }
YY_BREAK
case 29:
YY_RULE_SETUP
-#line 113 "lexer.l"
+#line 127 "lexer.l"
{ return _ALL_; }
YY_BREAK
case 30:
YY_RULE_SETUP
-#line 114 "lexer.l"
+#line 128 "lexer.l"
{ return _ANY_; }
YY_BREAK
case 31:
YY_RULE_SETUP
-#line 115 "lexer.l"
+#line 129 "lexer.l"
{ return _ENTRYPOINT_; }
YY_BREAK
case 32:
YY_RULE_SETUP
-#line 116 "lexer.l"
+#line 130 "lexer.l"
{ return _FILESIZE_; }
YY_BREAK
case 33:
YY_RULE_SETUP
-#line 117 "lexer.l"
+#line 131 "lexer.l"
{ return _MATCHES_; }
YY_BREAK
case 34:
YY_RULE_SETUP
-#line 118 "lexer.l"
+#line 132 "lexer.l"
{ return _CONTAINS_; }
YY_BREAK
case 35:
YY_RULE_SETUP
-#line 119 "lexer.l"
+#line 133 "lexer.l"
{ return _IMPORT_; }
YY_BREAK
case 36:
YY_RULE_SETUP
-#line 122 "lexer.l"
+#line 136 "lexer.l"
{ BEGIN(comment); }
YY_BREAK
case 37:
YY_RULE_SETUP
-#line 123 "lexer.l"
+#line 137 "lexer.l"
{ BEGIN(INITIAL); }
YY_BREAK
case 38:
/* rule 38 can match eol */
YY_RULE_SETUP
-#line 124 "lexer.l"
+#line 138 "lexer.l"
{ /* skip comments */ }
YY_BREAK
case 39:
YY_RULE_SETUP
-#line 127 "lexer.l"
+#line 141 "lexer.l"
{ /* skip single-line comments */ }
YY_BREAK
case 40:
YY_RULE_SETUP
-#line 130 "lexer.l"
+#line 144 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
yyextra->lex_buf_len = 0;
@@ -1223,12 +1237,12 @@ YY_RULE_SETUP
case 41:
/* rule 41 can match eol */
YY_RULE_SETUP
-#line 137 "lexer.l"
+#line 151 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 42:
YY_RULE_SETUP
-#line 140 "lexer.l"
+#line 154 "lexer.l"
{
char buffer[1024];
@@ -1334,7 +1348,7 @@ case YY_STATE_EOF(str):
case YY_STATE_EOF(regexp):
case YY_STATE_EOF(include):
case YY_STATE_EOF(comment):
-#line 242 "lexer.l"
+#line 256 "lexer.l"
{
YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
@@ -1356,7 +1370,7 @@ case YY_STATE_EOF(comment):
YY_BREAK
case 43:
YY_RULE_SETUP
-#line 262 "lexer.l"
+#line 276 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1372,7 +1386,7 @@ YY_RULE_SETUP
YY_BREAK
case 44:
YY_RULE_SETUP
-#line 276 "lexer.l"
+#line 290 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1388,7 +1402,7 @@ YY_RULE_SETUP
YY_BREAK
case 45:
YY_RULE_SETUP
-#line 290 "lexer.l"
+#line 304 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1405,7 +1419,7 @@ YY_RULE_SETUP
YY_BREAK
case 46:
YY_RULE_SETUP
-#line 305 "lexer.l"
+#line 319 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1422,7 +1436,7 @@ YY_RULE_SETUP
YY_BREAK
case 47:
YY_RULE_SETUP
-#line 320 "lexer.l"
+#line 334 "lexer.l"
{
char* text = yytext;
@@ -1463,7 +1477,7 @@ YY_RULE_SETUP
YY_BREAK
case 48:
YY_RULE_SETUP
-#line 359 "lexer.l"
+#line 373 "lexer.l"
{
if (strlen(yytext) > 128)
@@ -1484,7 +1498,7 @@ YY_RULE_SETUP
YY_BREAK
case 49:
YY_RULE_SETUP
-#line 378 "lexer.l"
+#line 392 "lexer.l"
{
#ifdef _MSC_VER
@@ -1506,7 +1520,7 @@ YY_RULE_SETUP
YY_BREAK
case 50:
YY_RULE_SETUP
-#line 397 "lexer.l"
+#line 411 "lexer.l"
{
yylval->double_ = atof(yytext);
return _DOUBLE_;
@@ -1514,7 +1528,7 @@ YY_RULE_SETUP
YY_BREAK
case 51:
YY_RULE_SETUP
-#line 402 "lexer.l"
+#line 416 "lexer.l"
{
yylval->integer = xtoi(yytext + 2);
@@ -1523,11 +1537,9 @@ YY_RULE_SETUP
YY_BREAK
case 52:
YY_RULE_SETUP
-#line 409 "lexer.l"
+#line 423 "lexer.l"
{ /* saw closing quote - all done */
- SIZED_STRING* s;
-
if (yyextra->lex_buf_len == 0)
{
yyerror(yyscanner, compiler, "empty string");
@@ -1536,10 +1548,7 @@ YY_RULE_SETUP
*yyextra->lex_buf_ptr = '\0';
BEGIN(INITIAL);
-
- s = (SIZED_STRING*) yr_malloc(yyextra->lex_buf_len + sizeof(SIZED_STRING));
- s->length = yyextra->lex_buf_len;
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, yyextra->lex_buf_len);
memcpy(s->c_string, yyextra->lex_buf, yyextra->lex_buf_len + 1);
yylval->sized_string = s;
@@ -1549,7 +1558,7 @@ YY_RULE_SETUP
YY_BREAK
case 53:
YY_RULE_SETUP
-#line 433 "lexer.l"
+#line 442 "lexer.l"
{
LEX_CHECK_SPACE_OK("\t", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1559,7 +1568,7 @@ YY_RULE_SETUP
YY_BREAK
case 54:
YY_RULE_SETUP
-#line 441 "lexer.l"
+#line 450 "lexer.l"
{
LEX_CHECK_SPACE_OK("\n", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1569,7 +1578,7 @@ YY_RULE_SETUP
YY_BREAK
case 55:
YY_RULE_SETUP
-#line 449 "lexer.l"
+#line 458 "lexer.l"
{
LEX_CHECK_SPACE_OK("\"", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1579,7 +1588,7 @@ YY_RULE_SETUP
YY_BREAK
case 56:
YY_RULE_SETUP
-#line 457 "lexer.l"
+#line 466 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1589,7 +1598,7 @@ YY_RULE_SETUP
YY_BREAK
case 57:
YY_RULE_SETUP
-#line 465 "lexer.l"
+#line 474 "lexer.l"
{
int result;
@@ -1602,13 +1611,13 @@ YY_RULE_SETUP
YY_BREAK
case 58:
YY_RULE_SETUP
-#line 476 "lexer.l"
+#line 485 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 59:
/* rule 59 can match eol */
YY_RULE_SETUP
-#line 479 "lexer.l"
+#line 488 "lexer.l"
{
yyerror(yyscanner, compiler, "unterminated string");
@@ -1618,7 +1627,7 @@ YY_RULE_SETUP
case 60:
/* rule 60 can match eol */
YY_RULE_SETUP
-#line 485 "lexer.l"
+#line 494 "lexer.l"
{
yyerror(yyscanner, compiler, "illegal escape sequence");
@@ -1626,11 +1635,9 @@ YY_RULE_SETUP
YY_BREAK
case 61:
YY_RULE_SETUP
-#line 491 "lexer.l"
+#line 500 "lexer.l"
{
- SIZED_STRING* s;
-
if (yyextra->lex_buf_len == 0)
{
yyerror(yyscanner, compiler, "empty regular expression");
@@ -1639,9 +1646,7 @@ YY_RULE_SETUP
*yyextra->lex_buf_ptr = '\0';
BEGIN(INITIAL);
-
- s = (SIZED_STRING*) yr_malloc(yyextra->lex_buf_len + sizeof(SIZED_STRING));
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, yyextra->lex_buf_len);
if (yytext[1] == 'i')
s->flags |= SIZED_STRING_FLAGS_NO_CASE;
@@ -1649,7 +1654,6 @@ YY_RULE_SETUP
if (yytext[1] == 's' || yytext[2] == 's')
s->flags |= SIZED_STRING_FLAGS_DOT_ALL;
- s->length = yyextra->lex_buf_len;
strlcpy(s->c_string, yyextra->lex_buf, s->length + 1);
yylval->sized_string = s;
@@ -1659,7 +1663,7 @@ YY_RULE_SETUP
YY_BREAK
case 62:
YY_RULE_SETUP
-#line 522 "lexer.l"
+#line 526 "lexer.l"
{
LEX_CHECK_SPACE_OK("/", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1669,7 +1673,7 @@ YY_RULE_SETUP
YY_BREAK
case 63:
YY_RULE_SETUP
-#line 530 "lexer.l"
+#line 534 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\.", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1680,13 +1684,13 @@ YY_RULE_SETUP
YY_BREAK
case 64:
YY_RULE_SETUP
-#line 539 "lexer.l"
+#line 543 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 65:
/* rule 65 can match eol */
YY_RULE_SETUP
-#line 542 "lexer.l"
+#line 546 "lexer.l"
{
yyerror(yyscanner, compiler, "unterminated regular expression");
@@ -1695,7 +1699,7 @@ YY_RULE_SETUP
YY_BREAK
case 66:
YY_RULE_SETUP
-#line 549 "lexer.l"
+#line 553 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1705,7 +1709,7 @@ YY_RULE_SETUP
YY_BREAK
case 67:
YY_RULE_SETUP
-#line 557 "lexer.l"
+#line 561 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1716,14 +1720,10 @@ YY_RULE_SETUP
case 68:
/* rule 68 can match eol */
YY_RULE_SETUP
-#line 565 "lexer.l"
+#line 569 "lexer.l"
{
- int len = strlen(yytext);
- SIZED_STRING* s = (SIZED_STRING*) yr_malloc(len + sizeof(SIZED_STRING));
-
- s->length = len;
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, strlen(yytext));
strlcpy(s->c_string, yytext, s->length + 1);
yylval->sized_string = s;
diff --git a/libyara/lexer.l b/libyara/lexer.l
index 9f9efd6..8bfb2f0 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -52,6 +52,20 @@ limitations under the License.
} \
}
+#define ALLOC_SIZED_STRING(str, str_len) \
+ SIZED_STRING* str = (SIZED_STRING*) yr_malloc( \
+ str_len + sizeof(SIZED_STRING)); \
+ if (str == NULL) \
+ { \
+ yyerror(yyscanner, compiler, "not enough memory"); \
+ yyterminate(); \
+ } \
+ else \
+ { \
+ str->length = (str_len); \
+ str->flags = 0; \
+ } \
+
#ifdef _WIN32
#define snprintf _snprintf
#endif
@@ -408,8 +422,6 @@ u?int(8|16|32)(be)? {
<str>\" { /* saw closing quote - all done */
- SIZED_STRING* s;
-
if (yyextra->lex_buf_len == 0)
{
yyerror(yyscanner, compiler, "empty string");
@@ -418,10 +430,7 @@ u?int(8|16|32)(be)? {
*yyextra->lex_buf_ptr = '\0';
BEGIN(INITIAL);
-
- s = (SIZED_STRING*) yr_malloc(yyextra->lex_buf_len + sizeof(SIZED_STRING));
- s->length = yyextra->lex_buf_len;
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, yyextra->lex_buf_len);
memcpy(s->c_string, yyextra->lex_buf, yyextra->lex_buf_len + 1);
yylval->sized_string = s;
@@ -490,8 +499,6 @@ u?int(8|16|32)(be)? {
<regexp>\/i?s? {
- SIZED_STRING* s;
-
if (yyextra->lex_buf_len == 0)
{
yyerror(yyscanner, compiler, "empty regular expression");
@@ -500,9 +507,7 @@ u?int(8|16|32)(be)? {
*yyextra->lex_buf_ptr = '\0';
BEGIN(INITIAL);
-
- s = (SIZED_STRING*) yr_malloc(yyextra->lex_buf_len + sizeof(SIZED_STRING));
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, yyextra->lex_buf_len);
if (yytext[1] == 'i')
s->flags |= SIZED_STRING_FLAGS_NO_CASE;
@@ -510,7 +515,6 @@ u?int(8|16|32)(be)? {
if (yytext[1] == 's' || yytext[2] == 's')
s->flags |= SIZED_STRING_FLAGS_DOT_ALL;
- s->length = yyextra->lex_buf_len;
strlcpy(s->c_string, yyextra->lex_buf, s->length + 1);
yylval->sized_string = s;
@@ -564,11 +568,7 @@ u?int(8|16|32)(be)? {
\{({hexdigit}|[ \-|\?\[\]\(\)\n\t])+\} {
- int len = strlen(yytext);
- SIZED_STRING* s = (SIZED_STRING*) yr_malloc(len + sizeof(SIZED_STRING));
-
- s->length = len;
- s->flags = 0;
+ ALLOC_SIZED_STRING(s, strlen(yytext));
strlcpy(s->c_string, yytext, s->length + 1);
yylval->sized_string = s;
diff --git a/libyara/modules.c b/libyara/modules.c
index 1c611c8..220cac5 100644
--- a/libyara/modules.c
+++ b/libyara/modules.c
@@ -133,12 +133,6 @@ int yr_modules_load(
NULL,
&module_structure));
- yr_hash_table_add(
- context->objects_table,
- module_name,
- NULL,
- module_structure);
-
mi.module_name = module_name;
mi.module_data = NULL;
mi.module_data_size = 0;
@@ -151,9 +145,17 @@ int yr_modules_load(
if (result == CALLBACK_ERROR)
return ERROR_CALLBACK_ERROR;
- yr_modules_do_declarations(
- module_name,
- module_structure);
+ FAIL_ON_ERROR_WITH_CLEANUP(
+ yr_modules_do_declarations(module_name, module_structure),
+ yr_object_destroy(module_structure));
+
+ FAIL_ON_ERROR_WITH_CLEANUP(
+ yr_hash_table_add(
+ context->objects_table,
+ module_name,
+ NULL,
+ module_structure),
+ yr_object_destroy(module_structure));
for (i = 0; i < sizeof(yr_modules_table) / sizeof(YR_MODULE); i++)
{
@@ -165,8 +167,10 @@ int yr_modules_load(
mi.module_data,
mi.module_data_size);
- if (result == ERROR_SUCCESS)
- yr_modules_table[i].is_loaded |= 1 << yr_get_tidx();
+ if (result != ERROR_SUCCESS)
+ return result;
+
+ yr_modules_table[i].is_loaded |= 1 << yr_get_tidx();
}
}
diff --git a/libyara/object.c b/libyara/object.c
index fbdaa4f..9bb8323 100644
--- a/libyara/object.c
+++ b/libyara/object.c
@@ -84,12 +84,6 @@ int yr_object_create(
obj->parent = parent;
obj->data = NULL;
- if (obj->identifier == NULL)
- {
- yr_free(obj);
- return ERROR_INSUFICIENT_MEMORY;
- }
-
switch(type)
{
case OBJECT_TYPE_STRUCTURE:
@@ -125,6 +119,12 @@ int yr_object_create(
break;
}
+ if (obj->identifier == NULL)
+ {
+ yr_free(obj);
+ return ERROR_INSUFICIENT_MEMORY;
+ }
+
if (parent != NULL)
{
assert(parent->type == OBJECT_TYPE_STRUCTURE ||
@@ -137,7 +137,10 @@ int yr_object_create(
case OBJECT_TYPE_STRUCTURE:
FAIL_ON_ERROR_WITH_CLEANUP(
yr_object_structure_set_member(parent, obj),
- yr_free(obj));
+ {
+ yr_free((void*) obj->identifier);
+ yr_free(obj);
+ });
break;
case OBJECT_TYPE_ARRAY:
@@ -166,7 +169,8 @@ int yr_object_function_create(
YR_OBJECT** function)
{
YR_OBJECT* return_obj;
- YR_OBJECT* f = NULL;
+ YR_OBJECT* o = NULL;
+ YR_OBJECT_FUNCTION* f = NULL;
int8_t return_type;
int i;
@@ -193,42 +197,44 @@ int yr_object_function_create(
// Try to find if the structure already has a function
// with that name. In that case this is a function oveload.
- f = yr_object_lookup_field(parent, identifier);
+ f = (YR_OBJECT_FUNCTION*) yr_object_lookup_field(parent, identifier);
- if (f != NULL && return_type != ((YR_OBJECT_FUNCTION*) f)->return_obj->type)
+ if (f != NULL && return_type != f->return_obj->type)
return ERROR_WRONG_RETURN_TYPE;
}
- if (f == NULL)
+ if (f == NULL) // Function doesn't exist yet
{
- // Function doesn't exist yet, create it.
+ // Let's create the result object first
- FAIL_ON_ERROR(yr_object_create(
- OBJECT_TYPE_FUNCTION,
- identifier,
- parent,
- &f));
+ FAIL_ON_ERROR(yr_object_create(return_type, "result", NULL, &return_obj));
FAIL_ON_ERROR_WITH_CLEANUP(
- yr_object_create(return_type, "result", f, &return_obj),
- yr_object_destroy(f));
-
- ((YR_OBJECT_FUNCTION*) f)->return_obj = return_obj;
+ yr_object_create(
+ OBJECT_TYPE_FUNCTION,
+ identifier,
+ parent,
+ &o),
+ yr_object_destroy(return_obj));
+
+ f = (YR_OBJECT_FUNCTION*) o;
+ f->return_obj = return_obj;
+ f->return_obj->parent = (YR_OBJECT*) f;
}
for (i = 0; i < MAX_OVERLOADED_FUNCTIONS; i++)
{
- if (((YR_OBJECT_FUNCTION*) f)->prototypes[i].arguments_fmt == NULL)
+ if (f->prototypes[i].arguments_fmt == NULL)
{
- ((YR_OBJECT_FUNCTION*) f)->prototypes[i].arguments_fmt = arguments_fmt;
- ((YR_OBJECT_FUNCTION*) f)->prototypes[i].code = code;
+ f->prototypes[i].arguments_fmt = arguments_fmt;
+ f->prototypes[i].code = code;
break;
}
}
if (function != NULL)
- *function = f;
+ *function = (YR_OBJECT*) f;
return ERROR_SUCCESS;
}
@@ -297,8 +303,11 @@ void yr_object_destroy(
YR_DICTIONARY_ITEMS* dict_items;
RE* re;
- int i;
SIZED_STRING* str;
+ int i;
+
+ if (object == NULL)
+ return;
switch(object->type)
{
@@ -936,7 +945,7 @@ SIZED_STRING* yr_object_get_string(
}
-void yr_object_set_integer(
+int yr_object_set_integer(
int64_t value,
YR_OBJECT* object,
const char* field,
@@ -959,10 +968,12 @@ void yr_object_set_integer(
assert(integer_obj->type == OBJECT_TYPE_INTEGER);
((YR_OBJECT_INTEGER*) integer_obj)->value = value;
+
+ return ERROR_SUCCESS;
}
-void yr_object_set_float(
+int yr_object_set_float(
double value,
YR_OBJECT* object,
const char* field,
@@ -985,10 +996,12 @@ void yr_object_set_float(
assert(double_obj->type == OBJECT_TYPE_FLOAT);
((YR_OBJECT_DOUBLE*) double_obj)->value = value;
+
+ return ERROR_SUCCESS;
}
-void yr_object_set_string(
+int yr_object_set_string(
const char* value,
size_t len,
YR_OBJECT* object,
@@ -1017,6 +1030,10 @@ void yr_object_set_string(
if (value != NULL)
{
string_obj->value = (SIZED_STRING*) yr_malloc(len + sizeof(SIZED_STRING));
+
+ if (string_obj->value == NULL)
+ return ERROR_INSUFICIENT_MEMORY;
+
string_obj->value->length = len;
string_obj->value->flags = 0;
@@ -1026,6 +1043,8 @@ void yr_object_set_string(
{
string_obj->value = NULL;
}
+
+ return ERROR_SUCCESS;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git
More information about the forensics-changes
mailing list