[Forensics-changes] [yara] 211/415: Improve error handling and fix memory leak

Hilko Bengen bengen at moszumanska.debian.org
Thu Apr 3 05:43:06 UTC 2014


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

bengen pushed a commit to branch debian
in repository yara.

commit 93b77fc973e00bb6511d4c99da325d05c0f65280
Author: Victor M. Alvarez <plusvic at gmail.com>
Date:   Tue Nov 5 13:16:13 2013 +0000

    Improve error handling and fix memory leak
---
 libyara/hex_grammar.c |  68 +++++++++++++++++----------
 libyara/hex_grammar.h |   2 +-
 libyara/hex_grammar.y |  30 ++++++++++--
 libyara/hex_lexer.c   |   2 +-
 libyara/hex_lexer.l   |   2 +-
 libyara/libyara.c     |   1 +
 libyara/re.c          |  18 ++++++++
 libyara/re_grammar.c  | 125 +++++++++++++++++++++++++++++++++++---------------
 libyara/re_grammar.h  |   2 +-
 libyara/re_grammar.y  |  57 +++++++++++++++++++++--
 libyara/re_lexer.c    |   2 +-
 libyara/re_lexer.l    |   2 +-
 12 files changed, 236 insertions(+), 75 deletions(-)

diff --git a/libyara/hex_grammar.c b/libyara/hex_grammar.c
index d69ecf5..4dc5174 100644
--- a/libyara/hex_grammar.c
+++ b/libyara/hex_grammar.c
@@ -109,6 +109,14 @@
 yydebug = 1;
 #endif
 
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
+
 
 
 /* Enabling traces.  */
@@ -131,13 +139,13 @@ yydebug = 1;
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 46 "hex_grammar.y"
+#line 54 "hex_grammar.y"
 {
   int integer;
   RE_NODE *re_node;
 }
 /* Line 193 of yacc.c.  */
-#line 141 "hex_grammar.c"
+#line 149 "hex_grammar.c"
 	YYSTYPE;
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
 # define YYSTYPE_IS_DECLARED 1
@@ -150,7 +158,7 @@ typedef union YYSTYPE
 
 
 /* Line 216 of yacc.c.  */
-#line 154 "hex_grammar.c"
+#line 162 "hex_grammar.c"
 
 #ifdef short
 # undef short
@@ -436,8 +444,8 @@ static const yytype_int8 yyrhs[] =
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
 static const yytype_uint8 yyrline[] =
 {
-       0,    59,    59,    67,    71,    78,    82,    86,    94,   104,
-     117,   121,   128,   151
+       0,    67,    67,    75,    79,    87,    91,    95,   103,   116,
+     132,   136,   145,   168
 };
 #endif
 
@@ -1354,7 +1362,7 @@ yyreduce:
   switch (yyn)
     {
         case 2:
-#line 60 "hex_grammar.y"
+#line 68 "hex_grammar.y"
     {
                 RE* re = yyget_extra(yyscanner);
                 re->root_node = (yyvsp[(2) - (3)].re_node);
@@ -1362,35 +1370,36 @@ yyreduce:
     break;
 
   case 3:
-#line 68 "hex_grammar.y"
+#line 76 "hex_grammar.y"
     {
             (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
          }
     break;
 
   case 4:
-#line 72 "hex_grammar.y"
+#line 80 "hex_grammar.y"
     {
             (yyval.re_node) = yr_re_node_create(RE_NODE_CONCAT, (yyvsp[(1) - (2)].re_node), (yyvsp[(2) - (2)].re_node));
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 5:
-#line 79 "hex_grammar.y"
+#line 88 "hex_grammar.y"
     {
           (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
         }
     break;
 
   case 6:
-#line 83 "hex_grammar.y"
+#line 92 "hex_grammar.y"
     {
           (yyval.re_node) = (yyvsp[(2) - (3)].re_node);
         }
     break;
 
   case 7:
-#line 87 "hex_grammar.y"
+#line 96 "hex_grammar.y"
     {
           mark_as_not_literal();
           (yyval.re_node) = (yyvsp[(2) - (3)].re_node);
@@ -1398,52 +1407,63 @@ yyreduce:
     break;
 
   case 8:
-#line 95 "hex_grammar.y"
+#line 104 "hex_grammar.y"
     {
           RE_NODE* re_any;
 
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
+
+          ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
           (yyval.re_node)->start = (yyvsp[(1) - (1)].integer);
           (yyval.re_node)->end = (yyvsp[(1) - (1)].integer);
         }
     break;
 
   case 9:
-#line 105 "hex_grammar.y"
+#line 117 "hex_grammar.y"
     {
           RE_NODE* re_any;
 
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
+
+          ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
           (yyval.re_node)->start = (yyvsp[(1) - (3)].integer);
           (yyval.re_node)->end = (yyvsp[(3) - (3)].integer);
         }
     break;
 
   case 10:
-#line 118 "hex_grammar.y"
+#line 133 "hex_grammar.y"
     {
                   (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
                }
     break;
 
   case 11:
-#line 122 "hex_grammar.y"
+#line 137 "hex_grammar.y"
     {
                   mark_as_not_literal();
                   (yyval.re_node) = yr_re_node_create(RE_NODE_ALT, (yyvsp[(1) - (3)].re_node), (yyvsp[(3) - (3)].re_node));
+
+                  ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
                }
     break;
 
   case 12:
-#line 129 "hex_grammar.y"
+#line 146 "hex_grammar.y"
     {
           RE* re = yyget_extra(yyscanner);
 
           (yyval.re_node) = yr_re_node_create(RE_NODE_LITERAL, NULL, NULL);
+
+          ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
           (yyval.re_node)->value = (yyvsp[(1) - (1)].integer);
 
           if (re->literal_string_len == re->literal_string_max)
@@ -1453,10 +1473,7 @@ yyreduce:
                 re->literal_string, 
                 re->literal_string_max);
 
-            if (re->literal_string == NULL)
-            {
-                //TODO
-            }
+            ERROR_IF(re->literal_string == NULL, ERROR_INSUFICIENT_MEMORY);
           }
 
           re->literal_string[re->literal_string_len] = (yyvsp[(1) - (1)].integer);
@@ -1465,7 +1482,7 @@ yyreduce:
     break;
 
   case 13:
-#line 152 "hex_grammar.y"
+#line 169 "hex_grammar.y"
     {
           uint8_t mask = (yyvsp[(1) - (1)].integer) >> 8;
 
@@ -1474,10 +1491,15 @@ yyreduce:
           if (mask == 0x00)
           {
             (yyval.re_node) = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
+            
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
           }
           else 
           {
             (yyval.re_node) = yr_re_node_create(RE_NODE_MASKED_LITERAL, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->value = (yyvsp[(1) - (1)].integer) & 0xFF;
             (yyval.re_node)->mask = mask;
           }
@@ -1486,7 +1508,7 @@ yyreduce:
 
 
 /* Line 1267 of yacc.c.  */
-#line 1490 "hex_grammar.c"
+#line 1512 "hex_grammar.c"
       default: break;
     }
   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
@@ -1700,7 +1722,7 @@ yyreturn:
 }
 
 
-#line 170 "hex_grammar.y"
+#line 192 "hex_grammar.y"
 
 
 
diff --git a/libyara/hex_grammar.h b/libyara/hex_grammar.h
index ee17efe..c38000e 100644
--- a/libyara/hex_grammar.h
+++ b/libyara/hex_grammar.h
@@ -54,7 +54,7 @@
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 46 "hex_grammar.y"
+#line 54 "hex_grammar.y"
 {
   int integer;
   RE_NODE *re_node;
diff --git a/libyara/hex_grammar.y b/libyara/hex_grammar.y
index f6e33b3..d6e029d 100644
--- a/libyara/hex_grammar.y
+++ b/libyara/hex_grammar.y
@@ -34,6 +34,14 @@ limitations under the License.
 yydebug = 1;
 #endif
 
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
+
 %}
 
 %debug
@@ -71,6 +79,7 @@ tokens : token
        | tokens token
          {
             $$ = yr_re_node_create(RE_NODE_CONCAT, $1, $2);
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        ;
 
@@ -98,6 +107,9 @@ range : _NUMBER_
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           $$ = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
+
+          ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
           $$->start = $1;
           $$->end = $1;
         }
@@ -108,6 +120,9 @@ range : _NUMBER_
           re_any = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
 
           $$ = yr_re_node_create(RE_NODE_RANGE, re_any, NULL);
+
+          ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
           $$->start = $1;
           $$->end = $3;
         }
@@ -122,6 +137,8 @@ alternatives : tokens
                {
                   mark_as_not_literal();
                   $$ = yr_re_node_create(RE_NODE_ALT, $1, $3);
+
+                  ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
                }
              ;
 
@@ -130,6 +147,9 @@ byte  : _BYTE_
           RE* re = yyget_extra(yyscanner);
 
           $$ = yr_re_node_create(RE_NODE_LITERAL, NULL, NULL);
+
+          ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
           $$->value = $1;
 
           if (re->literal_string_len == re->literal_string_max)
@@ -139,10 +159,7 @@ byte  : _BYTE_
                 re->literal_string, 
                 re->literal_string_max);
 
-            if (re->literal_string == NULL)
-            {
-                //TODO
-            }
+            ERROR_IF(re->literal_string == NULL, ERROR_INSUFICIENT_MEMORY);
           }
 
           re->literal_string[re->literal_string_len] = $1;
@@ -157,10 +174,15 @@ byte  : _BYTE_
           if (mask == 0x00)
           {
             $$ = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
+            
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
           }
           else 
           {
             $$ = yr_re_node_create(RE_NODE_MASKED_LITERAL, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->value = $1 & 0xFF;
             $$->mask = mask;
           }
diff --git a/libyara/hex_lexer.c b/libyara/hex_lexer.c
index 891aabf..605e4c0 100644
--- a/libyara/hex_lexer.c
+++ b/libyara/hex_lexer.c
@@ -2137,7 +2137,7 @@ int yr_parse_hex_string(
   yyparse(yyscanner);
   hex_yylex_destroy(yyscanner);
 
-  return ERROR_SUCCESS;
+  return (*re)->error_code;
 }
 
 
diff --git a/libyara/hex_lexer.l b/libyara/hex_lexer.l
index 916935b..39cc6a7 100644
--- a/libyara/hex_lexer.l
+++ b/libyara/hex_lexer.l
@@ -150,7 +150,7 @@ int yr_parse_hex_string(
   yyparse(yyscanner);
   yylex_destroy(yyscanner);
 
-  return ERROR_SUCCESS;
+  return (*re)->error_code;
 }
 
 
diff --git a/libyara/libyara.c b/libyara/libyara.c
index e83e926..94746cb 100644
--- a/libyara/libyara.c
+++ b/libyara/libyara.c
@@ -61,6 +61,7 @@ void yr_initialize(void)
 void yr_finalize(void)
 {
   yr_heap_free();
+  yr_re_finalize();
 }
 
 //
diff --git a/libyara/re.c b/libyara/re.c
index 1477240..5a8bac4 100644
--- a/libyara/re.c
+++ b/libyara/re.c
@@ -73,6 +73,23 @@ int yr_re_initialize()
 }
 
 
+int yr_re_finalize()
+{
+  RE_THREAD_STORAGE* thread_storage;
+
+  #ifdef WIN32
+  thread_storage = TlsGetValue(thread_storage_key);
+  #else
+  thread_storage = pthread_getspecific(thread_storage_key);
+  #endif
+
+  if (thread_storage != NULL)
+    yr_free(thread_storage);
+
+  return ERROR_SUCCESS;
+}
+
+
 RE_NODE* yr_re_node_create(
     int type, 
     RE_NODE* left, 
@@ -129,6 +146,7 @@ int yr_re_create(
   (*re)->flags = 0;
   (*re)->root_node = NULL;
   (*re)->error_message = NULL;
+  (*re)->error_code = ERROR_SUCCESS;
 
   return ERROR_SUCCESS;
 }
diff --git a/libyara/re_grammar.c b/libyara/re_grammar.c
index 3017832..eec1169 100644
--- a/libyara/re_grammar.c
+++ b/libyara/re_grammar.c
@@ -123,6 +123,15 @@ yydebug = 1;
 
 #define mark_as_not_literal() \
     ((RE*) yyget_extra(yyscanner))->flags &= ~RE_FLAGS_LITERAL_STRING
+
+
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
                 
 
 
@@ -146,7 +155,7 @@ yydebug = 1;
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 51 "re_grammar.y"
+#line 60 "re_grammar.y"
 {
   int integer;
   uint32_t range;
@@ -154,7 +163,7 @@ typedef union YYSTYPE
   uint8_t* class_vector;
 }
 /* Line 193 of yacc.c.  */
-#line 158 "re_grammar.c"
+#line 167 "re_grammar.c"
 	YYSTYPE;
 # define yystype YYSTYPE /* obsolescent; will be withdrawn */
 # define YYSTYPE_IS_DECLARED 1
@@ -167,7 +176,7 @@ typedef union YYSTYPE
 
 
 /* Line 216 of yacc.c.  */
-#line 171 "re_grammar.c"
+#line 180 "re_grammar.c"
 
 #ifdef short
 # undef short
@@ -455,11 +464,11 @@ static const yytype_int8 yyrhs[] =
 };
 
 /* YYRLINE[YYN] -- source line where rule number YYN was defined.  */
-static const yytype_uint8 yyrline[] =
+static const yytype_uint16 yyrline[] =
 {
-       0,    77,    77,    82,    85,    89,    96,   100,   106,   111,
-     117,   122,   128,   135,   143,   150,   156,   160,   165,   188,
-     193,   198,   203,   208,   213,   218
+       0,    86,    86,    91,    94,    98,   107,   111,   119,   126,
+     135,   142,   151,   161,   172,   182,   188,   192,   199,   222,
+     229,   236,   243,   250,   257,   264
 };
 #endif
 
@@ -1081,9 +1090,9 @@ yydestruct (yymsg, yytype, yyvaluep, yyscanner, lex_env)
   switch (yytype)
     {
       case 6: /* "_CLASS_" */
-#line 70 "re_grammar.y"
+#line 79 "re_grammar.y"
 	{ yr_free((yyvaluep->class_vector)); };
-#line 1087 "re_grammar.c"
+#line 1096 "re_grammar.c"
 	break;
 
       default:
@@ -1393,7 +1402,7 @@ yyreduce:
   switch (yyn)
     {
         case 2:
-#line 78 "re_grammar.y"
+#line 87 "re_grammar.y"
     {
         RE* re = yyget_extra(yyscanner);
         re->root_node = (yyvsp[(1) - (1)].re_node);
@@ -1401,83 +1410,103 @@ yyreduce:
     break;
 
   case 4:
-#line 86 "re_grammar.y"
+#line 95 "re_grammar.y"
     {
                 (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
               }
     break;
 
   case 5:
-#line 90 "re_grammar.y"
+#line 99 "re_grammar.y"
     {
                 mark_as_not_literal();
                 (yyval.re_node) = yr_re_node_create(RE_NODE_ALT, (yyvsp[(1) - (3)].re_node), (yyvsp[(3) - (3)].re_node));
+
+                ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
               }
     break;
 
   case 6:
-#line 97 "re_grammar.y"
+#line 108 "re_grammar.y"
     {
                   (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
                 }
     break;
 
   case 7:
-#line 101 "re_grammar.y"
+#line 112 "re_grammar.y"
     {
                   (yyval.re_node) = yr_re_node_create(RE_NODE_CONCAT, (yyvsp[(1) - (2)].re_node), (yyvsp[(2) - (2)].re_node));
+
+                  ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
                 }
     break;
 
   case 8:
-#line 107 "re_grammar.y"
+#line 120 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_STAR, (yyvsp[(1) - (2)].re_node), NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 9:
-#line 112 "re_grammar.y"
+#line 127 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_STAR, (yyvsp[(1) - (3)].re_node), NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->greedy = FALSE;
          }
     break;
 
   case 10:
-#line 118 "re_grammar.y"
+#line 136 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_PLUS, (yyvsp[(1) - (2)].re_node), NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 11:
-#line 123 "re_grammar.y"
+#line 143 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_PLUS, (yyvsp[(1) - (3)].re_node), NULL);
+            
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->greedy = FALSE;
          }
     break;
 
   case 12:
-#line 129 "re_grammar.y"
+#line 152 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, (yyvsp[(1) - (2)].re_node), NULL);
+            
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->start = 0;
             (yyval.re_node)->end = 1;
          }
     break;
 
   case 13:
-#line 136 "re_grammar.y"
+#line 162 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, (yyvsp[(1) - (3)].re_node), NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->start = 0;
             (yyval.re_node)->end = 1;
             (yyval.re_node)->greedy = FALSE;
@@ -1485,43 +1514,51 @@ yyreduce:
     break;
 
   case 14:
-#line 144 "re_grammar.y"
+#line 173 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_RANGE, (yyvsp[(1) - (2)].re_node), NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->start = (yyvsp[(2) - (2)].range) & 0xFFFF;;
             (yyval.re_node)->end = (yyvsp[(2) - (2)].range) >> 16;;
          }
     break;
 
   case 15:
-#line 151 "re_grammar.y"
+#line 183 "re_grammar.y"
     {
             (yyval.re_node) = (yyvsp[(1) - (1)].re_node);
          }
     break;
 
   case 16:
-#line 157 "re_grammar.y"
+#line 189 "re_grammar.y"
     {
             (yyval.re_node) = (yyvsp[(2) - (3)].re_node);
          }
     break;
 
   case 17:
-#line 161 "re_grammar.y"
+#line 193 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 18:
-#line 166 "re_grammar.y"
+#line 200 "re_grammar.y"
     {
             RE* re = yyget_extra(yyscanner);
 
             (yyval.re_node) = yr_re_node_create(RE_NODE_LITERAL, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->value = (yyvsp[(1) - (1)].integer);
 
             if (re->literal_string_len == re->literal_string_max)
@@ -1531,10 +1568,7 @@ yyreduce:
                   re->literal_string,
                   re->literal_string_max);
 
-              if (re->literal_string == NULL)
-              {
-                //TODO
-              }
+              ERROR_IF(re->literal_string == NULL, ERROR_INSUFICIENT_MEMORY);
             }
 
             re->literal_string[re->literal_string_len] = (yyvsp[(1) - (1)].integer);
@@ -1543,65 +1577,80 @@ yyreduce:
     break;
 
   case 19:
-#line 189 "re_grammar.y"
+#line 223 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_WORD_CHAR, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 20:
-#line 194 "re_grammar.y"
+#line 230 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_NON_WORD_CHAR, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 21:
-#line 199 "re_grammar.y"
+#line 237 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_SPACE, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 22:
-#line 204 "re_grammar.y"
+#line 244 "re_grammar.y"
     {  
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_NON_SPACE, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 23:
-#line 209 "re_grammar.y"
+#line 251 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_DIGIT, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 24:
-#line 214 "re_grammar.y"
+#line 258 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_NON_DIGIT, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
          }
     break;
 
   case 25:
-#line 219 "re_grammar.y"
+#line 265 "re_grammar.y"
     {
             mark_as_not_literal();
             (yyval.re_node) = yr_re_node_create(RE_NODE_CLASS, NULL, NULL);
+
+            ERROR_IF((yyval.re_node) == NULL, ERROR_INSUFICIENT_MEMORY);
+
             (yyval.re_node)->class_vector = (yyvsp[(1) - (1)].class_vector);
          }
     break;
 
 
 /* Line 1267 of yacc.c.  */
-#line 1605 "re_grammar.c"
+#line 1654 "re_grammar.c"
       default: break;
     }
   YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
@@ -1815,7 +1864,7 @@ yyreturn:
 }
 
 
-#line 227 "re_grammar.y"
+#line 276 "re_grammar.y"
 
 
 
diff --git a/libyara/re_grammar.h b/libyara/re_grammar.h
index 4dfc586..ec0ec17 100644
--- a/libyara/re_grammar.h
+++ b/libyara/re_grammar.h
@@ -68,7 +68,7 @@
 
 #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
 typedef union YYSTYPE
-#line 51 "re_grammar.y"
+#line 60 "re_grammar.y"
 {
   int integer;
   uint32_t range;
diff --git a/libyara/re_grammar.y b/libyara/re_grammar.y
index 55c9076..d0b0d95 100644
--- a/libyara/re_grammar.y
+++ b/libyara/re_grammar.y
@@ -34,6 +34,15 @@ yydebug = 1;
 
 #define mark_as_not_literal() \
     ((RE*) yyget_extra(yyscanner))->flags &= ~RE_FLAGS_LITERAL_STRING
+
+
+#define ERROR_IF(x, error) \
+    if (x) \
+    { \
+      RE* re = yyget_extra(yyscanner); \
+      re->error_code = error; \
+      YYABORT; \
+    } \
                 
 %}
 
@@ -90,6 +99,8 @@ alternative : concatenation
               {
                 mark_as_not_literal();
                 $$ = yr_re_node_create(RE_NODE_ALT, $1, $3);
+
+                ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
               }
             ;
 
@@ -100,6 +111,8 @@ concatenation : repeat
               | concatenation repeat
                 {
                   $$ = yr_re_node_create(RE_NODE_CONCAT, $1, $2);
+
+                  ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
                 }
               ;
 
@@ -107,28 +120,41 @@ repeat : single '*'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_STAR, $1, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | single '*' '?'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_STAR, $1, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->greedy = FALSE;
          }
        | single '+'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_PLUS, $1, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | single '+' '?'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_PLUS, $1, NULL);
+            
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->greedy = FALSE;
          }
        | single '?'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_RANGE, $1, NULL);
+            
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->start = 0;
             $$->end = 1;
          }  
@@ -136,6 +162,9 @@ repeat : single '*'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_RANGE, $1, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->start = 0;
             $$->end = 1;
             $$->greedy = FALSE;
@@ -144,6 +173,9 @@ repeat : single '*'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_RANGE, $1, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->start = $2 & 0xFFFF;;
             $$->end = $2 >> 16;;
          }
@@ -161,12 +193,17 @@ single : '(' alternative ')'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_ANY, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _CHAR_
          {
             RE* re = yyget_extra(yyscanner);
 
             $$ = yr_re_node_create(RE_NODE_LITERAL, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->value = $1;
 
             if (re->literal_string_len == re->literal_string_max)
@@ -176,10 +213,7 @@ single : '(' alternative ')'
                   re->literal_string,
                   re->literal_string_max);
 
-              if (re->literal_string == NULL)
-              {
-                //TODO
-              }
+              ERROR_IF(re->literal_string == NULL, ERROR_INSUFICIENT_MEMORY);
             }
 
             re->literal_string[re->literal_string_len] = $1;
@@ -189,36 +223,51 @@ single : '(' alternative ')'
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_WORD_CHAR, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _NON_WORD_CHAR_
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_NON_WORD_CHAR, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _SPACE_
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_SPACE, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _NON_SPACE_
          {  
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_NON_SPACE, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _DIGIT_
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_DIGIT, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _NON_DIGIT_
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_NON_DIGIT, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
          }
        | _CLASS_
          {
             mark_as_not_literal();
             $$ = yr_re_node_create(RE_NODE_CLASS, NULL, NULL);
+
+            ERROR_IF($$ == NULL, ERROR_INSUFICIENT_MEMORY);
+
             $$->class_vector = $1;
          }
        ;
diff --git a/libyara/re_lexer.c b/libyara/re_lexer.c
index 161c671..736cfe9 100644
--- a/libyara/re_lexer.c
+++ b/libyara/re_lexer.c
@@ -2533,7 +2533,7 @@ int yr_parse_re_string(
     return ERROR_INVALID_REGULAR_EXPRESSION;
   }
 
-  return ERROR_SUCCESS;
+  return (*re)->error_code;
 }
 
 
diff --git a/libyara/re_lexer.l b/libyara/re_lexer.l
index 66b7ab0..eeeaabf 100644
--- a/libyara/re_lexer.l
+++ b/libyara/re_lexer.l
@@ -487,7 +487,7 @@ int yr_parse_re_string(
     return ERROR_INVALID_REGULAR_EXPRESSION;
   }
 
-  return ERROR_SUCCESS;
+  return (*re)->error_code;
 }
 
 

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