[Forensics-changes] [yara] 291/415: Fix issue #45: libyara should never call exit()
Hilko Bengen
bengen at moszumanska.debian.org
Thu Apr 3 05:43:16 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 907c469200d3b6f2821e49bc1981689694fd3947
Author: Victor Manuel Alvarez <vmalvarez at virustotal.com>
Date: Thu Dec 5 18:28:55 2013 +0100
Fix issue #45: libyara should never call exit()
---
libyara/compiler.c | 6 ++
libyara/hex_lexer.c | 33 +++++++++
libyara/hex_lexer.h | 6 ++
libyara/hex_lexer.l | 33 +++++++++
libyara/lexer.c | 195 +++++++++++++++++++++++++++++-----------------------
libyara/lexer.h | 8 +++
libyara/lexer.l | 24 ++++++-
libyara/libyara.c | 26 ++++---
libyara/parser.c | 2 +-
libyara/re_lexer.c | 43 ++++++++++--
libyara/re_lexer.h | 7 ++
libyara/re_lexer.l | 34 +++++++++
libyara/yara.h | 4 ++
13 files changed, 319 insertions(+), 102 deletions(-)
diff --git a/libyara/compiler.c b/libyara/compiler.c
index 8cfe879..63f423c 100644
--- a/libyara/compiler.c
+++ b/libyara/compiler.c
@@ -752,6 +752,12 @@ char* yr_compiler_get_error_message(
buffer_size,
"loop nesting limit exceeded");
break;
+ case ERROR_INTERNAL_FATAL_ERROR:
+ snprintf(
+ buffer,
+ buffer_size,
+ "internal fatal error");
+ break;
}
return buffer;
diff --git a/libyara/hex_lexer.c b/libyara/hex_lexer.c
index c48cbf9..a7e4ef4 100644
--- a/libyara/hex_lexer.c
+++ b/libyara/hex_lexer.c
@@ -2080,6 +2080,29 @@ void hex_yyfree (void * ptr , yyscan_t yyscanner)
+
+#ifdef WIN32
+extern DWORD recovery_state_key;
+#else
+extern pthread_key_t recovery_state_key;
+#endif
+
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ jmp_buf* recovery_state;
+
+ #ifdef WIN32
+ recovery_state = TlsGetValue(recovery_state_key) ;
+ #else
+ recovery_state = pthread_getspecific(recovery_state_key);
+ #endif
+
+ longjmp(*recovery_state, 1);
+}
+
void yyerror(
yyscan_t yyscanner,
LEX_ENVIRONMENT* lex_env,
@@ -2096,6 +2119,7 @@ int yr_parse_hex_string(
RE** re)
{
yyscan_t yyscanner;
+ jmp_buf recovery_state;
LEX_ENVIRONMENT lex_env;
lex_env.last_error_message = NULL;
@@ -2116,6 +2140,15 @@ int yr_parse_hex_string(
(*re)->flags |= RE_FLAGS_LITERAL_STRING;
(*re)->flags |= RE_FLAGS_FAST_HEX_REGEXP;
+ #ifdef WIN32
+ TlsSetValue(recovery_state_key, (LPVOID) &recovery_state);
+ #else
+ pthread_setspecific(recovery_state_key, (void*) &recovery_state);
+ #endif
+
+ if (setjmp(recovery_state) != 0)
+ return ERROR_INTERNAL_FATAL_ERROR;
+
hex_yylex_init(&yyscanner);
hex_yyset_extra(*re,yyscanner);
hex_yy_scan_string(hex_string,yyscanner);
diff --git a/libyara/hex_lexer.h b/libyara/hex_lexer.h
index a9bb2b5..eaeda59 100644
--- a/libyara/hex_lexer.h
+++ b/libyara/hex_lexer.h
@@ -20,6 +20,7 @@ limitations under the License.
#define yyparse hex_yyparse
#define yylex hex_yylex
#define yyerror hex_yyerror
+#define yyfatal hex_yyfatal
#define yychar hex_yychar
#define yydebug hex_yydebug
#define yynerrs hex_yynerrs
@@ -43,6 +44,8 @@ typedef struct _LEX_ENVIRONMENT
} LEX_ENVIRONMENT;
+#define YY_FATAL_ERROR(msg) hex_yyfatal(yyscanner, msg)
+
#define LEX_ENV ((LEX_ENVIRONMENT*) lex_env)
#define YY_DECL int hex_yylex \
@@ -66,3 +69,6 @@ void yyerror(
LEX_ENVIRONMENT* lex_env,
const char *error_message);
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message);
diff --git a/libyara/hex_lexer.l b/libyara/hex_lexer.l
index d18ba08..96f7591 100644
--- a/libyara/hex_lexer.l
+++ b/libyara/hex_lexer.l
@@ -137,6 +137,29 @@ hexdigit [a-fA-F0-9]
%%
+
+#ifdef WIN32
+extern DWORD recovery_state_key;
+#else
+extern pthread_key_t recovery_state_key;
+#endif
+
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ jmp_buf* recovery_state;
+
+ #ifdef WIN32
+ recovery_state = TlsGetValue(recovery_state_key) ;
+ #else
+ recovery_state = pthread_getspecific(recovery_state_key);
+ #endif
+
+ longjmp(*recovery_state, 1);
+}
+
void yyerror(
yyscan_t yyscanner,
LEX_ENVIRONMENT* lex_env,
@@ -153,6 +176,7 @@ int yr_parse_hex_string(
RE** re)
{
yyscan_t yyscanner;
+ jmp_buf recovery_state;
LEX_ENVIRONMENT lex_env;
lex_env.last_error_message = NULL;
@@ -173,6 +197,15 @@ int yr_parse_hex_string(
(*re)->flags |= RE_FLAGS_LITERAL_STRING;
(*re)->flags |= RE_FLAGS_FAST_HEX_REGEXP;
+ #ifdef WIN32
+ TlsSetValue(recovery_state_key, (LPVOID) &recovery_state);
+ #else
+ pthread_setspecific(recovery_state_key, (void*) &recovery_state);
+ #endif
+
+ if (setjmp(recovery_state) != 0)
+ return ERROR_INTERNAL_FATAL_ERROR;
+
yylex_init(&yyscanner);
yyset_extra(*re, yyscanner);
yy_scan_string(hex_string, yyscanner);
diff --git a/libyara/lexer.c b/libyara/lexer.c
index 5c34846..5bd9d18 100644
--- a/libyara/lexer.c
+++ b/libyara/lexer.c
@@ -47,6 +47,7 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
+typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@@ -357,7 +358,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
*/
#define YY_DO_BEFORE_ACTION \
yyg->yytext_ptr = yy_bp; \
- yyleng = (size_t) (yy_cp - yy_bp); \
+ yyleng = (yy_size_t) (yy_cp - yy_bp); \
yyg->yy_hold_char = *yy_cp; \
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
@@ -642,6 +643,7 @@ limitations under the License.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
+#include <setjmp.h>
#include "yara.h"
#include "sizedstr.h"
@@ -679,7 +681,7 @@ limitations under the License.
-#line 683 "lexer.c"
+#line 685 "lexer.c"
#define INITIAL 0
#define str 1
@@ -914,10 +916,10 @@ YY_DECL
register int yy_act;
struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
-#line 79 "lexer.l"
+#line 80 "lexer.l"
-#line 921 "lexer.c"
+#line 923 "lexer.c"
yylval = yylval_param;
@@ -1016,253 +1018,253 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
-#line 81 "lexer.l"
+#line 82 "lexer.l"
{ return _LT_; }
YY_BREAK
case 2:
YY_RULE_SETUP
-#line 82 "lexer.l"
+#line 83 "lexer.l"
{ return _GT_; }
YY_BREAK
case 3:
YY_RULE_SETUP
-#line 83 "lexer.l"
+#line 84 "lexer.l"
{ return _LE_; }
YY_BREAK
case 4:
YY_RULE_SETUP
-#line 84 "lexer.l"
+#line 85 "lexer.l"
{ return _GE_; }
YY_BREAK
case 5:
YY_RULE_SETUP
-#line 85 "lexer.l"
+#line 86 "lexer.l"
{ return _EQ_; }
YY_BREAK
case 6:
YY_RULE_SETUP
-#line 86 "lexer.l"
+#line 87 "lexer.l"
{ return _NEQ_; }
YY_BREAK
case 7:
YY_RULE_SETUP
-#line 87 "lexer.l"
+#line 88 "lexer.l"
{ return _SHIFT_LEFT_; }
YY_BREAK
case 8:
YY_RULE_SETUP
-#line 88 "lexer.l"
+#line 89 "lexer.l"
{ return _SHIFT_RIGHT_; }
YY_BREAK
case 9:
YY_RULE_SETUP
-#line 89 "lexer.l"
+#line 90 "lexer.l"
{ return _PRIVATE_; }
YY_BREAK
case 10:
YY_RULE_SETUP
-#line 90 "lexer.l"
+#line 91 "lexer.l"
{ return _GLOBAL_; }
YY_BREAK
case 11:
YY_RULE_SETUP
-#line 91 "lexer.l"
+#line 92 "lexer.l"
{ return _RULE_; }
YY_BREAK
case 12:
YY_RULE_SETUP
-#line 92 "lexer.l"
+#line 93 "lexer.l"
{ return _META_; }
YY_BREAK
case 13:
YY_RULE_SETUP
-#line 93 "lexer.l"
+#line 94 "lexer.l"
{ return _STRINGS_; }
YY_BREAK
case 14:
YY_RULE_SETUP
-#line 94 "lexer.l"
+#line 95 "lexer.l"
{ return _ASCII_; }
YY_BREAK
case 15:
YY_RULE_SETUP
-#line 95 "lexer.l"
+#line 96 "lexer.l"
{ return _WIDE_; }
YY_BREAK
case 16:
YY_RULE_SETUP
-#line 96 "lexer.l"
+#line 97 "lexer.l"
{ return _FULLWORD_; }
YY_BREAK
case 17:
YY_RULE_SETUP
-#line 97 "lexer.l"
+#line 98 "lexer.l"
{ return _NOCASE_; }
YY_BREAK
case 18:
YY_RULE_SETUP
-#line 98 "lexer.l"
+#line 99 "lexer.l"
{ return _CONDITION_; }
YY_BREAK
case 19:
YY_RULE_SETUP
-#line 99 "lexer.l"
+#line 100 "lexer.l"
{ return _TRUE_; }
YY_BREAK
case 20:
YY_RULE_SETUP
-#line 100 "lexer.l"
+#line 101 "lexer.l"
{ return _FALSE_; }
YY_BREAK
case 21:
YY_RULE_SETUP
-#line 101 "lexer.l"
+#line 102 "lexer.l"
{ return _NOT_; }
YY_BREAK
case 22:
YY_RULE_SETUP
-#line 102 "lexer.l"
+#line 103 "lexer.l"
{ return _AND_; }
YY_BREAK
case 23:
YY_RULE_SETUP
-#line 103 "lexer.l"
+#line 104 "lexer.l"
{ return _OR_; }
YY_BREAK
case 24:
YY_RULE_SETUP
-#line 104 "lexer.l"
+#line 105 "lexer.l"
{ return _AT_; }
YY_BREAK
case 25:
YY_RULE_SETUP
-#line 105 "lexer.l"
+#line 106 "lexer.l"
{ return _IN_; }
YY_BREAK
case 26:
YY_RULE_SETUP
-#line 106 "lexer.l"
+#line 107 "lexer.l"
{ return _OF_; }
YY_BREAK
case 27:
YY_RULE_SETUP
-#line 107 "lexer.l"
+#line 108 "lexer.l"
{ return _THEM_; }
YY_BREAK
case 28:
YY_RULE_SETUP
-#line 108 "lexer.l"
+#line 109 "lexer.l"
{ return _FOR_; }
YY_BREAK
case 29:
YY_RULE_SETUP
-#line 109 "lexer.l"
+#line 110 "lexer.l"
{ return _ALL_; }
YY_BREAK
case 30:
YY_RULE_SETUP
-#line 110 "lexer.l"
+#line 111 "lexer.l"
{ return _ANY_; }
YY_BREAK
case 31:
YY_RULE_SETUP
-#line 111 "lexer.l"
+#line 112 "lexer.l"
{ return _ENTRYPOINT_; }
YY_BREAK
case 32:
YY_RULE_SETUP
-#line 112 "lexer.l"
+#line 113 "lexer.l"
{ return _SIZE_; }
YY_BREAK
case 33:
YY_RULE_SETUP
-#line 113 "lexer.l"
+#line 114 "lexer.l"
{ return _RVA_; }
YY_BREAK
case 34:
YY_RULE_SETUP
-#line 114 "lexer.l"
+#line 115 "lexer.l"
{ return _OFFSET_; }
YY_BREAK
case 35:
YY_RULE_SETUP
-#line 115 "lexer.l"
+#line 116 "lexer.l"
{ return _FILE_; }
YY_BREAK
case 36:
YY_RULE_SETUP
-#line 116 "lexer.l"
+#line 117 "lexer.l"
{ return _SECTION_; }
YY_BREAK
case 37:
YY_RULE_SETUP
-#line 117 "lexer.l"
+#line 118 "lexer.l"
{ return _UINT8_; }
YY_BREAK
case 38:
YY_RULE_SETUP
-#line 118 "lexer.l"
+#line 119 "lexer.l"
{ return _UINT16_; }
YY_BREAK
case 39:
YY_RULE_SETUP
-#line 119 "lexer.l"
+#line 120 "lexer.l"
{ return _UINT32_; }
YY_BREAK
case 40:
YY_RULE_SETUP
-#line 120 "lexer.l"
+#line 121 "lexer.l"
{ return _INT8_; }
YY_BREAK
case 41:
YY_RULE_SETUP
-#line 121 "lexer.l"
+#line 122 "lexer.l"
{ return _INT16_; }
YY_BREAK
case 42:
YY_RULE_SETUP
-#line 122 "lexer.l"
+#line 123 "lexer.l"
{ return _INT32_; }
YY_BREAK
case 43:
YY_RULE_SETUP
-#line 123 "lexer.l"
+#line 124 "lexer.l"
{ return _MATCHES_; }
YY_BREAK
case 44:
YY_RULE_SETUP
-#line 124 "lexer.l"
+#line 125 "lexer.l"
{ return _CONTAINS_; }
YY_BREAK
case 45:
YY_RULE_SETUP
-#line 125 "lexer.l"
+#line 126 "lexer.l"
{ return _INDEX_; }
YY_BREAK
case 46:
YY_RULE_SETUP
-#line 128 "lexer.l"
+#line 129 "lexer.l"
{ BEGIN(comment); }
YY_BREAK
case 47:
YY_RULE_SETUP
-#line 129 "lexer.l"
+#line 130 "lexer.l"
{ BEGIN(INITIAL); }
YY_BREAK
case 48:
/* rule 48 can match eol */
YY_RULE_SETUP
-#line 130 "lexer.l"
+#line 131 "lexer.l"
{ /* skip comments */ }
YY_BREAK
case 49:
YY_RULE_SETUP
-#line 133 "lexer.l"
+#line 134 "lexer.l"
{ /* skip single-line comments */ }
YY_BREAK
case 50:
YY_RULE_SETUP
-#line 136 "lexer.l"
+#line 137 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
yyextra->lex_buf_len = 0;
@@ -1272,12 +1274,12 @@ YY_RULE_SETUP
case 51:
/* rule 51 can match eol */
YY_RULE_SETUP
-#line 143 "lexer.l"
+#line 144 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 52:
YY_RULE_SETUP
-#line 146 "lexer.l"
+#line 147 "lexer.l"
{
char buffer[1024];
@@ -1379,7 +1381,7 @@ case YY_STATE_EOF(str):
case YY_STATE_EOF(regexp):
case YY_STATE_EOF(include):
case YY_STATE_EOF(comment):
-#line 244 "lexer.l"
+#line 245 "lexer.l"
{
YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
@@ -1401,7 +1403,7 @@ case YY_STATE_EOF(comment):
YY_BREAK
case 53:
YY_RULE_SETUP
-#line 264 "lexer.l"
+#line 265 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1410,7 +1412,7 @@ YY_RULE_SETUP
YY_BREAK
case 54:
YY_RULE_SETUP
-#line 271 "lexer.l"
+#line 272 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1419,7 +1421,7 @@ YY_RULE_SETUP
YY_BREAK
case 55:
YY_RULE_SETUP
-#line 278 "lexer.l"
+#line 279 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1429,7 +1431,7 @@ YY_RULE_SETUP
YY_BREAK
case 56:
YY_RULE_SETUP
-#line 286 "lexer.l"
+#line 287 "lexer.l"
{
yylval->c_string = yr_strdup(yytext);
@@ -1439,7 +1441,7 @@ YY_RULE_SETUP
YY_BREAK
case 57:
YY_RULE_SETUP
-#line 294 "lexer.l"
+#line 295 "lexer.l"
{
if (strlen(yytext) > 128)
@@ -1453,7 +1455,7 @@ YY_RULE_SETUP
YY_BREAK
case 58:
YY_RULE_SETUP
-#line 306 "lexer.l"
+#line 307 "lexer.l"
{
yylval->integer = (size_t) atol(yytext);
@@ -1471,7 +1473,7 @@ YY_RULE_SETUP
YY_BREAK
case 59:
YY_RULE_SETUP
-#line 322 "lexer.l"
+#line 323 "lexer.l"
{
yylval->integer = xtoi(yytext + 2);
@@ -1480,7 +1482,7 @@ YY_RULE_SETUP
YY_BREAK
case 60:
YY_RULE_SETUP
-#line 329 "lexer.l"
+#line 330 "lexer.l"
{ /* saw closing quote - all done */
SIZED_STRING* s;
@@ -1504,7 +1506,7 @@ YY_RULE_SETUP
YY_BREAK
case 61:
YY_RULE_SETUP
-#line 351 "lexer.l"
+#line 352 "lexer.l"
{
LEX_CHECK_SPACE_OK("\t", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1514,7 +1516,7 @@ YY_RULE_SETUP
YY_BREAK
case 62:
YY_RULE_SETUP
-#line 359 "lexer.l"
+#line 360 "lexer.l"
{
LEX_CHECK_SPACE_OK("\"", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1524,7 +1526,7 @@ YY_RULE_SETUP
YY_BREAK
case 63:
YY_RULE_SETUP
-#line 367 "lexer.l"
+#line 368 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1534,7 +1536,7 @@ YY_RULE_SETUP
YY_BREAK
case 64:
YY_RULE_SETUP
-#line 375 "lexer.l"
+#line 376 "lexer.l"
{
int result;
@@ -1547,13 +1549,13 @@ YY_RULE_SETUP
YY_BREAK
case 65:
YY_RULE_SETUP
-#line 386 "lexer.l"
+#line 387 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 66:
/* rule 66 can match eol */
YY_RULE_SETUP
-#line 389 "lexer.l"
+#line 390 "lexer.l"
{
yyerror(yyscanner, "unterminated string");
@@ -1563,7 +1565,7 @@ YY_RULE_SETUP
case 67:
/* rule 67 can match eol */
YY_RULE_SETUP
-#line 395 "lexer.l"
+#line 396 "lexer.l"
{
yyerror(yyscanner, "illegal escape sequence");
@@ -1571,7 +1573,7 @@ YY_RULE_SETUP
YY_BREAK
case 68:
YY_RULE_SETUP
-#line 401 "lexer.l"
+#line 402 "lexer.l"
{
SIZED_STRING* s;
@@ -1597,7 +1599,7 @@ YY_RULE_SETUP
YY_BREAK
case 69:
YY_RULE_SETUP
-#line 425 "lexer.l"
+#line 426 "lexer.l"
{
LEX_CHECK_SPACE_OK("/", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1607,7 +1609,7 @@ YY_RULE_SETUP
YY_BREAK
case 70:
YY_RULE_SETUP
-#line 433 "lexer.l"
+#line 434 "lexer.l"
{
LEX_CHECK_SPACE_OK("\\.", yyextra->lex_buf_len, LEX_BUF_SIZE);
@@ -1618,13 +1620,13 @@ YY_RULE_SETUP
YY_BREAK
case 71:
YY_RULE_SETUP
-#line 442 "lexer.l"
+#line 443 "lexer.l"
{ YYTEXT_TO_BUFFER; }
YY_BREAK
case 72:
/* rule 72 can match eol */
YY_RULE_SETUP
-#line 445 "lexer.l"
+#line 446 "lexer.l"
{
yyerror(yyscanner, "unterminated regular expression");
@@ -1633,7 +1635,7 @@ YY_RULE_SETUP
YY_BREAK
case 73:
YY_RULE_SETUP
-#line 452 "lexer.l"
+#line 453 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1643,7 +1645,7 @@ YY_RULE_SETUP
YY_BREAK
case 74:
YY_RULE_SETUP
-#line 460 "lexer.l"
+#line 461 "lexer.l"
{
yyextra->lex_buf_ptr = yyextra->lex_buf;
@@ -1654,7 +1656,7 @@ YY_RULE_SETUP
case 75:
/* rule 75 can match eol */
YY_RULE_SETUP
-#line 468 "lexer.l"
+#line 469 "lexer.l"
{
int len = strlen(yytext);
@@ -1670,12 +1672,12 @@ YY_RULE_SETUP
case 76:
/* rule 76 can match eol */
YY_RULE_SETUP
-#line 481 "lexer.l"
+#line 482 "lexer.l"
/* skip whitespace */
YY_BREAK
case 77:
YY_RULE_SETUP
-#line 483 "lexer.l"
+#line 484 "lexer.l"
{
if (yytext[0] >= 32 && yytext[0] < 127)
@@ -1691,10 +1693,10 @@ YY_RULE_SETUP
YY_BREAK
case 78:
YY_RULE_SETUP
-#line 496 "lexer.l"
+#line 497 "lexer.l"
ECHO;
YY_BREAK
-#line 1698 "lexer.c"
+#line 1700 "lexer.c"
case YY_END_OF_BUFFER:
{
@@ -2827,11 +2829,25 @@ void yara_yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables"
-#line 496 "lexer.l"
+#line 497 "lexer.l"
+
+
+
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
+ yyerror(yyscanner, error_message);
+ longjmp(compiler->fatal_error, 1);
+}
-void yyerror(yyscan_t yyscanner, const char *error_message)
+void yyerror(
+ yyscan_t yyscanner,
+ const char *error_message)
{
YR_COMPILER* compiler = yara_yyget_extra(yyscanner);
@@ -2903,6 +2919,9 @@ int yr_lex_parse_rules_string(
yyscan_t yyscanner;
YY_BUFFER_STATE state;
+ if (setjmp(compiler->fatal_error) != 0)
+ return compiler->errors;
+
yara_yylex_init(&yyscanner);
yara_yyset_debug(1,yyscanner);
@@ -2924,6 +2943,10 @@ int yr_lex_parse_rules_file(
YR_COMPILER* compiler)
{
yyscan_t yyscanner;
+
+ if (setjmp(compiler->fatal_error) != 0)
+ return compiler->errors;
+
yara_yylex_init(&yyscanner);
#if YYDEBUG
diff --git a/libyara/lexer.h b/libyara/lexer.h
index d974e3b..1aac3f8 100644
--- a/libyara/lexer.h
+++ b/libyara/lexer.h
@@ -21,6 +21,7 @@ limitations under the License.
#define yyparse yara_yyparse
#define yylex yara_yylex
#define yyerror yara_yyerror
+#define yyfatal yara_yyfatal
#define yychar yara_yychar
#define yydebug yara_yydebug
#define yynerrs yara_yynerrs
@@ -33,6 +34,9 @@ limitations under the License.
typedef void* yyscan_t;
#endif
+#define YY_FATAL_ERROR(msg) yara_yyfatal(yyscanner, msg)
+
+
#define YY_EXTRA_TYPE YR_COMPILER*
#define YY_USE_CONST
@@ -49,6 +53,10 @@ void yyerror(
yyscan_t yyscanner,
const char *error_message);
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message);
+
YY_EXTRA_TYPE yyget_extra(
yyscan_t yyscanner);
diff --git a/libyara/lexer.l b/libyara/lexer.l
index 2654c1d..a3541bc 100644
--- a/libyara/lexer.l
+++ b/libyara/lexer.l
@@ -22,6 +22,7 @@ limitations under the License.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
+#include <setjmp.h>
#include "yara.h"
#include "sizedstr.h"
@@ -495,7 +496,21 @@ $({letter}|{digit}|_)* {
%%
-void yyerror(yyscan_t yyscanner, const char *error_message)
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ YR_COMPILER* compiler = yyget_extra(yyscanner);
+
+ yyerror(yyscanner, error_message);
+ longjmp(compiler->fatal_error, 1);
+}
+
+
+void yyerror(
+ yyscan_t yyscanner,
+ const char *error_message)
{
YR_COMPILER* compiler = yyget_extra(yyscanner);
@@ -567,6 +582,9 @@ int yr_lex_parse_rules_string(
yyscan_t yyscanner;
YY_BUFFER_STATE state;
+ if (setjmp(compiler->fatal_error) != 0)
+ return compiler->errors;
+
yylex_init(&yyscanner);
yyset_debug(1, yyscanner);
@@ -588,6 +606,10 @@ int yr_lex_parse_rules_file(
YR_COMPILER* compiler)
{
yyscan_t yyscanner;
+
+ if (setjmp(compiler->fatal_error) != 0)
+ return compiler->errors;
+
yylex_init(&yyscanner);
#if YYDEBUG
diff --git a/libyara/libyara.c b/libyara/libyara.c
index dcfa7aa..648a0fc 100644
--- a/libyara/libyara.c
+++ b/libyara/libyara.c
@@ -35,9 +35,11 @@ char lowercase[256];
char altercase[256];
#ifdef WIN32
-DWORD key;
+DWORD tidx_key;
+DWORD recovery_state_key;
#else
-pthread_key_t key;
+pthread_key_t tidx_key;
+pthread_key_t recovery_state_key;
#endif
@@ -67,9 +69,11 @@ void yr_initialize(void)
yr_heap_alloc();
#ifdef WIN32
- key = TlsAlloc();
+ tidx_key = TlsAlloc();
+ recovery_state_key = TlsAlloc();
#else
- pthread_key_create(&key, NULL);
+ pthread_key_create(&tidx_key, NULL);
+ pthread_key_create(&recovery_state_key, NULL);
#endif
yr_re_initialize();
@@ -101,9 +105,11 @@ void yr_finalize(void)
yr_re_finalize_thread();
#ifdef WIN32
- TlsFree(key);
+ TlsFree(tidx_key);
+ TlsFree(recovery_state_key);
#else
- pthread_key_delete(key);
+ pthread_key_delete(tidx_key);
+ pthread_key_delete(recovery_state_key);
#endif
yr_re_finalize();
@@ -125,9 +131,9 @@ void yr_finalize(void)
void yr_set_tidx(int tidx)
{
#ifdef WIN32
- TlsSetValue(key, (LPVOID) (tidx + 1));
+ TlsSetValue(tidx_key, (LPVOID) (tidx + 1));
#else
- pthread_setspecific(key, (void*) (size_t) (tidx + 1));
+ pthread_setspecific(tidx_key, (void*) (size_t) (tidx + 1));
#endif
}
@@ -145,8 +151,8 @@ void yr_set_tidx(int tidx)
int yr_get_tidx(void)
{
#ifdef WIN32
- return (int) TlsGetValue(key) - 1;
+ return (int) TlsGetValue(tidx_key) - 1;
#else
- return (int) (size_t) pthread_getspecific(key) - 1;
+ return (int) (size_t) pthread_getspecific(tidx_key) - 1;
#endif
}
diff --git a/libyara/parser.c b/libyara/parser.c
index 4ec5757..df8e6e5 100644
--- a/libyara/parser.c
+++ b/libyara/parser.c
@@ -273,7 +273,7 @@ YR_STRING* yr_parser_reduce_string_declaration(
snprintf(
message,
sizeof(message),
- "invalid %s in string \"%s\": %s",
+ "invalid %s \"%s\": %s",
(flags & STRING_GFLAGS_HEXADECIMAL) ?
"hex string" : "regular expression",
identifier,
diff --git a/libyara/re_lexer.c b/libyara/re_lexer.c
index 11b0aa9..b6ab684 100644
--- a/libyara/re_lexer.c
+++ b/libyara/re_lexer.c
@@ -47,6 +47,7 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t;
+typedef uint64_t flex_uint64_t;
#else
typedef signed char flex_int8_t;
typedef short int flex_int16_t;
@@ -357,7 +358,7 @@ static void yy_fatal_error (yyconst char msg[] ,yyscan_t yyscanner );
*/
#define YY_DO_BEFORE_ACTION \
yyg->yytext_ptr = yy_bp; \
- yyleng = (size_t) (yy_cp - yy_bp); \
+ yyleng = (yy_size_t) (yy_cp - yy_bp); \
yyg->yy_hold_char = *yy_cp; \
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
@@ -515,7 +516,7 @@ uint8_t read_escaped_char(yyscan_t yyscanner);
#define YY_NO_UNISTD_H 1
-#line 519 "re_lexer.c"
+#line 520 "re_lexer.c"
#define INITIAL 0
#define char_class 1
@@ -752,7 +753,7 @@ YY_DECL
#line 55 "re_lexer.l"
-#line 756 "re_lexer.c"
+#line 757 "re_lexer.c"
yylval = yylval_param;
@@ -1219,7 +1220,7 @@ YY_RULE_SETUP
#line 363 "re_lexer.l"
ECHO;
YY_BREAK
-#line 1223 "re_lexer.c"
+#line 1224 "re_lexer.c"
case YY_END_OF_BUFFER:
{
@@ -2441,6 +2442,30 @@ uint8_t read_escaped_char(yyscan_t yyscanner)
}
+
+#ifdef WIN32
+extern DWORD recovery_state_key;
+#else
+extern pthread_key_t recovery_state_key;
+#endif
+
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ jmp_buf* recovery_state;
+
+ #ifdef WIN32
+ recovery_state = TlsGetValue(recovery_state_key) ;
+ #else
+ recovery_state = pthread_getspecific(recovery_state_key);
+ #endif
+
+ longjmp(*recovery_state, 1);
+}
+
+
void yyerror(
yyscan_t yyscanner,
LEX_ENVIRONMENT* lex_env,
@@ -2458,6 +2483,7 @@ int yr_parse_re_string(
RE** re)
{
yyscan_t yyscanner;
+ jmp_buf recovery_state;
LEX_ENVIRONMENT lex_env;
lex_env.last_error_message = NULL;
@@ -2472,6 +2498,15 @@ int yr_parse_re_string(
(*re)->flags |= RE_FLAGS_LITERAL_STRING;
+ #ifdef WIN32
+ TlsSetValue(recovery_state_key, (LPVOID) &recovery_state);
+ #else
+ pthread_setspecific(recovery_state_key, (void*) &recovery_state);
+ #endif
+
+ if (setjmp(recovery_state) != 0)
+ return ERROR_INTERNAL_FATAL_ERROR;
+
re_yylex_init(&yyscanner);
re_yyset_extra(*re,yyscanner);
re_yy_scan_string(re_string,yyscanner);
diff --git a/libyara/re_lexer.h b/libyara/re_lexer.h
index 5327ac2..fdfd942 100644
--- a/libyara/re_lexer.h
+++ b/libyara/re_lexer.h
@@ -20,6 +20,7 @@ limitations under the License.
#define yyparse re_yyparse
#define yylex re_yylex
#define yyerror re_yyerror
+#define yyfatal re_yyfatal
#define yychar re_yychar
#define yydebug re_yydebug
#define yynerrs re_yynerrs
@@ -47,6 +48,8 @@ typedef struct _LEX_ENVIRONMENT
#define LEX_ENV ((LEX_ENVIRONMENT*) lex_env)
+#define YY_FATAL_ERROR(msg) re_yyfatal(yyscanner, msg)
+
#define YY_DECL int re_yylex \
(YYSTYPE * yylval_param , yyscan_t yyscanner, LEX_ENVIRONMENT* lex_env)
@@ -68,6 +71,10 @@ void yyerror(
LEX_ENVIRONMENT* lex_env,
const char *error_message);
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message);
+
int yr_parse_re_string(
const char* re_string,
RE** re);
diff --git a/libyara/re_lexer.l b/libyara/re_lexer.l
index 050b487..5e06a39 100644
--- a/libyara/re_lexer.l
+++ b/libyara/re_lexer.l
@@ -405,6 +405,30 @@ uint8_t read_escaped_char(yyscan_t yyscanner)
}
+
+#ifdef WIN32
+extern DWORD recovery_state_key;
+#else
+extern pthread_key_t recovery_state_key;
+#endif
+
+
+void yyfatal(
+ yyscan_t yyscanner,
+ const char *error_message)
+{
+ jmp_buf* recovery_state;
+
+ #ifdef WIN32
+ recovery_state = TlsGetValue(recovery_state_key) ;
+ #else
+ recovery_state = pthread_getspecific(recovery_state_key);
+ #endif
+
+ longjmp(*recovery_state, 1);
+}
+
+
void yyerror(
yyscan_t yyscanner,
LEX_ENVIRONMENT* lex_env,
@@ -422,6 +446,7 @@ int yr_parse_re_string(
RE** re)
{
yyscan_t yyscanner;
+ jmp_buf recovery_state;
LEX_ENVIRONMENT lex_env;
lex_env.last_error_message = NULL;
@@ -436,6 +461,15 @@ int yr_parse_re_string(
(*re)->flags |= RE_FLAGS_LITERAL_STRING;
+ #ifdef WIN32
+ TlsSetValue(recovery_state_key, (LPVOID) &recovery_state);
+ #else
+ pthread_setspecific(recovery_state_key, (void*) &recovery_state);
+ #endif
+
+ if (setjmp(recovery_state) != 0)
+ return ERROR_INTERNAL_FATAL_ERROR;
+
yylex_init(&yyscanner);
yyset_extra(*re, yyscanner);
yy_scan_string(re_string, yyscanner);
diff --git a/libyara/yara.h b/libyara/yara.h
index 63a9adf..78ce28b 100644
--- a/libyara/yara.h
+++ b/libyara/yara.h
@@ -19,6 +19,7 @@ limitations under the License.
#include <stdio.h>
#include <stdint.h>
+#include <setjmp.h>
#ifdef WIN32
#include <windows.h>
@@ -88,6 +89,7 @@ typedef pthread_mutex_t mutex_t;
#define ERROR_LOOP_NESTING_LIMIT_EXCEEDED 32
#define ERROR_DUPLICATE_LOOP_IDENTIFIER 33
#define ERROR_TOO_MANY_SCAN_THREADS 34
+#define ERROR_INTERNAL_FATAL_ERROR 35
#define CALLBACK_MSG_RULE_MATCHING 1
@@ -505,6 +507,8 @@ typedef struct _YR_COMPILER
int last_error;
int last_error_line;
+ jmp_buf fatal_error;
+
YR_ARENA* sz_arena;
YR_ARENA* rules_arena;
YR_ARENA* strings_arena;
--
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