[Forensics-changes] [yara] 96/160: Handle empty strings correctly (issue #303)

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:29:22 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.4.0
in repository yara.

commit d90463455d91855cb040bd1d291fd25ee6aa71cf
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Wed Apr 29 13:00:07 2015 +0200

    Handle empty strings correctly (issue #303)
---
 libyara/compiler.c           |  7 +++++++
 libyara/include/yara/error.h |  1 +
 libyara/lexer.c              | 47 ++++++++++++++++++++------------------------
 libyara/lexer.l              |  5 -----
 libyara/modules/tests.c      | 10 ++++++++++
 libyara/parser.c             |  7 +++++++
 yara-python/tests.py         |  2 ++
 7 files changed, 48 insertions(+), 31 deletions(-)

diff --git a/libyara/compiler.c b/libyara/compiler.c
index fa0730f..60743e1 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -769,6 +769,13 @@ YR_API char* yr_compiler_get_error_message(
           "unreferenced string \"%s\"",
           compiler->last_error_extra_info);
       break;
+    case ERROR_EMPTY_STRING:
+      snprintf(
+          buffer,
+          buffer_size,
+          "empty string \"%s\"",
+          compiler->last_error_extra_info);
+      break;
     case ERROR_NOT_A_STRUCTURE:
       snprintf(
           buffer,
diff --git a/libyara/include/yara/error.h b/libyara/include/yara/error.h
index d5e772c..c907b83 100644
--- a/libyara/include/yara/error.h
+++ b/libyara/include/yara/error.h
@@ -64,6 +64,7 @@ limitations under the License.
 #define ERROR_WRONG_ARGUMENTS                   40
 #define ERROR_WRONG_RETURN_TYPE                 41
 #define ERROR_DUPLICATED_STRUCTURE_MEMBER       42
+#define ERROR_EMPTY_STRING                      43
 
 
 #define FAIL_ON_ERROR(x) { \
diff --git a/libyara/lexer.c b/libyara/lexer.c
index 4c0a72b..92bce24 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -1540,11 +1540,6 @@ YY_RULE_SETUP
 #line 423 "lexer.l"
 {     /* saw closing quote - all done */
 
-  if (yyextra->lex_buf_len == 0)
-  {
-    yyerror(yyscanner, compiler, "empty string");
-  }
-
   *yyextra->lex_buf_ptr = '\0';
 
   BEGIN(INITIAL);
@@ -1558,7 +1553,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 53:
 YY_RULE_SETUP
-#line 442 "lexer.l"
+#line 437 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("\t", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1568,7 +1563,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 54:
 YY_RULE_SETUP
-#line 450 "lexer.l"
+#line 445 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("\n", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1578,7 +1573,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 55:
 YY_RULE_SETUP
-#line 458 "lexer.l"
+#line 453 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("\"", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1588,7 +1583,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 56:
 YY_RULE_SETUP
-#line 466 "lexer.l"
+#line 461 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("\\", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1598,7 +1593,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 57:
 YY_RULE_SETUP
-#line 474 "lexer.l"
+#line 469 "lexer.l"
 {
 
    int result;
@@ -1611,13 +1606,13 @@ YY_RULE_SETUP
 	YY_BREAK
 case 58:
 YY_RULE_SETUP
-#line 485 "lexer.l"
+#line 480 "lexer.l"
 { YYTEXT_TO_BUFFER; }
 	YY_BREAK
 case 59:
 /* rule 59 can match eol */
 YY_RULE_SETUP
-#line 488 "lexer.l"
+#line 483 "lexer.l"
 {
 
   yyerror(yyscanner, compiler, "unterminated string");
@@ -1627,7 +1622,7 @@ YY_RULE_SETUP
 case 60:
 /* rule 60 can match eol */
 YY_RULE_SETUP
-#line 494 "lexer.l"
+#line 489 "lexer.l"
 {
 
   yyerror(yyscanner, compiler, "illegal escape sequence");
@@ -1635,7 +1630,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 61:
 YY_RULE_SETUP
-#line 500 "lexer.l"
+#line 495 "lexer.l"
 {
 
   if (yyextra->lex_buf_len == 0)
@@ -1663,7 +1658,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 62:
 YY_RULE_SETUP
-#line 526 "lexer.l"
+#line 521 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("/", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1673,7 +1668,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 63:
 YY_RULE_SETUP
-#line 534 "lexer.l"
+#line 529 "lexer.l"
 {
 
   LEX_CHECK_SPACE_OK("\\.", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1684,13 +1679,13 @@ YY_RULE_SETUP
 	YY_BREAK
 case 64:
 YY_RULE_SETUP
-#line 543 "lexer.l"
+#line 538 "lexer.l"
 { YYTEXT_TO_BUFFER; }
 	YY_BREAK
 case 65:
 /* rule 65 can match eol */
 YY_RULE_SETUP
-#line 546 "lexer.l"
+#line 541 "lexer.l"
 {
 
   yyerror(yyscanner, compiler, "unterminated regular expression");
@@ -1699,7 +1694,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 66:
 YY_RULE_SETUP
-#line 553 "lexer.l"
+#line 548 "lexer.l"
 {
 
   yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1709,7 +1704,7 @@ YY_RULE_SETUP
 	YY_BREAK
 case 67:
 YY_RULE_SETUP
-#line 561 "lexer.l"
+#line 556 "lexer.l"
 {
 
   yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1720,7 +1715,7 @@ YY_RULE_SETUP
 case 68:
 /* rule 68 can match eol */
 YY_RULE_SETUP
-#line 569 "lexer.l"
+#line 564 "lexer.l"
 {
 
   ALLOC_SIZED_STRING(s, strlen(yytext));
@@ -1734,12 +1729,12 @@ YY_RULE_SETUP
 case 69:
 /* rule 69 can match eol */
 YY_RULE_SETUP
-#line 580 "lexer.l"
+#line 575 "lexer.l"
 /* skip whitespace */
 	YY_BREAK
 case 70:
 YY_RULE_SETUP
-#line 582 "lexer.l"
+#line 577 "lexer.l"
 {
 
   if (yytext[0] >= 32 && yytext[0] < 127)
@@ -1755,10 +1750,10 @@ YY_RULE_SETUP
 	YY_BREAK
 case 71:
 YY_RULE_SETUP
-#line 595 "lexer.l"
+#line 590 "lexer.l"
 ECHO;
 	YY_BREAK
-#line 1762 "lexer.c"
+#line 1757 "lexer.c"
 
 	case YY_END_OF_BUFFER:
 		{
@@ -2888,7 +2883,7 @@ void yara_yyfree (void * ptr , yyscan_t yyscanner)
 
 #define YYTABLES_NAME "yytables"
 
-#line 595 "lexer.l"
+#line 590 "lexer.l"
 
 
 
diff --git a/libyara/lexer.l b/libyara/lexer.l
index 8bfb2f0..d8acb00 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -422,11 +422,6 @@ u?int(8|16|32)(be)? {
 
 <str>\"   {     /* saw closing quote - all done */
 
-  if (yyextra->lex_buf_len == 0)
-  {
-    yyerror(yyscanner, compiler, "empty string");
-  }
-
   *yyextra->lex_buf_ptr = '\0';
 
   BEGIN(INITIAL);
diff --git a/libyara/modules/tests.c b/libyara/modules/tests.c
index ebf8970..60a8255 100644
--- a/libyara/modules/tests.c
+++ b/libyara/modules/tests.c
@@ -62,12 +62,20 @@ define_function(length)
   return_integer(strlen(s));
 }
 
+
+define_function(empty)
+{
+  return_string("");
+}
+
+
 begin_declarations;
 
   begin_struct("constants");
     declare_integer("one");
     declare_integer("two");
     declare_string("foo");
+    declare_string("empty");
   end_struct("constants");
 
   begin_struct("undefined");
@@ -96,6 +104,7 @@ begin_declarations;
   declare_function("fsum", "ff", "f", fsum_2);
   declare_function("fsum", "fff", "f", fsum_3);
   declare_function("length", "s", "i", length);
+  declare_function("empty", "", "s", empty);
 
 end_declarations;
 
@@ -122,6 +131,7 @@ int module_load(
   set_integer(1, module_object, "constants.one");
   set_integer(2, module_object, "constants.two");
   set_string("foo", module_object, "constants.foo");
+  set_string("", module_object, "constants.empty");
 
   set_integer(1, module_object, "struct_array[1].i");
 
diff --git a/libyara/parser.c b/libyara/parser.c
index c07afa8..f656988 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -463,6 +463,13 @@ YR_STRING* yr_parser_reduce_string_declaration(
 
   RE_ERROR re_error;
 
+  if (str->length == 0)
+  {
+    compiler->last_result = ERROR_EMPTY_STRING;
+    yr_compiler_set_error_extra_info(compiler, identifier);
+    goto _exit;
+  }
+
   if (str->flags & SIZED_STRING_FLAGS_NO_CASE)
     string_flags |= STRING_GFLAGS_NO_CASE;
 
diff --git a/yara-python/tests.py b/yara-python/tests.py
index f67e299..24720d7 100644
--- a/yara-python/tests.py
+++ b/yara-python/tests.py
@@ -800,6 +800,8 @@ class TestYara(unittest.TestCase):
         self.assertTrueRules([
             'import "tests" rule test { condition: tests.constants.one + 1 == tests.constants.two }',
             'import "tests" rule test { condition: tests.constants.foo == "foo" }',
+            'import "tests" rule test { condition: tests.constants.empty == "" }',
+            'import "tests" rule test { condition: tests.empty() == "" }',
             'import "tests" rule test { condition: tests.struct_array[1].i == 1 }',
             'import "tests" rule test { condition: tests.struct_array[0].i == 1 or true}',
             'import "tests" rule test { condition: tests.integer_array[0] == 0}',

-- 
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