diff --git a/libclamav/jsparse/js-norm.c b/libclamav/jsparse/js-norm.c index 11689d5ae..e1d630107 100644 --- a/libclamav/jsparse/js-norm.c +++ b/libclamav/jsparse/js-norm.c @@ -57,18 +57,21 @@ enum tokenizer_state { Number, DoubleQString, SingleQString, - Identifier + Identifier, + Dummy }; typedef struct scanner { - enum tokenizer_state state; struct text_buffer buf; const char *yytext; size_t yylen; const char *in; size_t insize; size_t pos; + size_t lastpos; + enum tokenizer_state state; + enum tokenizer_state last_state; } *yyscan_t; typedef int YY_BUFFER_STATE; @@ -1328,9 +1331,13 @@ static inline int parseId(YYSTYPE *lvalp, yyscan_t scanner) textbuffer_putc(&scanner->buf, c); break; } + if(scanner->pos == scanner->insize) { + scanner->pos++; + } /* else fallthrough */ default: /* character is no longer part of identifier */ + scanner->state = Initial; textbuffer_putc(&scanner->buf, '\0'); scanner->pos--; kw = in_word_set(scanner->buf.data, scanner->buf.pos-1); @@ -1389,6 +1396,8 @@ static int yy_scan_bytes(const char *p, size_t len, yyscan_t scanner) scanner->in = p; scanner->insize = len; scanner->pos = 0; + scanner->lastpos = -1; + scanner->last_state = Dummy; return 0; } @@ -1421,6 +1430,16 @@ static int yylex(YYSTYPE *lvalp, yyscan_t scanner) scanner->yytext = NULL; scanner->yylen = 0; + if(scanner->pos == scanner->lastpos) { + if(scanner->last_state == scanner->state) { + cli_dbgmsg(MODULE "infloop detected, skipping character\n"); + scanner->pos++; + } + /* its not necesarely an infloop if it changed + * state, and it shouldn't infloop between states */ + } + scanner->lastpos = scanner->pos; + scanner->last_state = scanner->state; while(scanner->pos < scanner->insize) { switch(scanner->state) { case Initial: diff --git a/unit_tests/check_jsnorm.c b/unit_tests/check_jsnorm.c index cd859d362..82bc37438 100644 --- a/unit_tests/check_jsnorm.c +++ b/unit_tests/check_jsnorm.c @@ -384,6 +384,13 @@ static const char jstest_buf12[] = static const char jstest_expected12[] = "var n000=\"test\x1test\";"; +static const char jstest_buf13[] = +"var x\\s12345"; + +static const char jstest_expected13[] = +"var n000"; + + static struct { const char *in; const char *expected; @@ -400,7 +407,8 @@ static struct { {jstest_buf9, jstest_expected9}, {jstest_buf10, jstest_expected10}, {jstest_buf11, jstest_expected11}, - {jstest_buf12, jstest_expected12} + {jstest_buf12, jstest_expected12}, + {jstest_buf13, jstest_expected13} }; #ifdef CHECK_HAVE_LOOPS