Skip to content

Commit 7aacb8f

Browse files
committed
Remove recursion limit from RegExp engine.
Related issue: #388 JerryScript-DCO-1.0-Signed-off-by: László Langó llango.u-szeged@partner.samsung.com
1 parent 9cc706d commit 7aacb8f

File tree

4 files changed

+0
-66
lines changed

4 files changed

+0
-66
lines changed

jerry-core/ecma/operations/ecma-regexp-object.cpp

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
288288
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
289289
re_opcode_t op;
290290

291-
if (re_ctx_p->recursion_depth >= RE_EXECUTE_RECURSION_LIMIT)
292-
{
293-
ret_value = ecma_raise_range_error ("RegExp executor recursion limit is exceeded.");
294-
return ret_value;
295-
}
296-
re_ctx_p->recursion_depth++;
297-
298291
while ((op = re_get_opcode (&bc_p)))
299292
{
300293
switch (op)
@@ -303,15 +296,13 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
303296
{
304297
JERRY_DDLOG ("Execute RE_OP_MATCH: match\n");
305298
*out_iter_p = iter;
306-
re_ctx_p->recursion_depth--;
307299
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE);
308300
return ret_value; /* match */
309301
}
310302
case RE_OP_CHAR:
311303
{
312304
if (lit_utf8_iterator_is_eos (&iter))
313305
{
314-
re_ctx_p->recursion_depth--;
315306
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
316307
}
317308

@@ -323,7 +314,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
323314
if (ch1 != ch2)
324315
{
325316
JERRY_DDLOG ("fail\n");
326-
re_ctx_p->recursion_depth--;
327317
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
328318
}
329319

@@ -335,7 +325,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
335325
{
336326
if (lit_utf8_iterator_is_eos (&iter))
337327
{
338-
re_ctx_p->recursion_depth--;
339328
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
340329
}
341330

@@ -345,7 +334,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
345334
if (lit_char_is_line_terminator (ch))
346335
{
347336
JERRY_DDLOG ("fail\n");
348-
re_ctx_p->recursion_depth--;
349337
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
350338
}
351339

@@ -365,7 +353,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
365353
if (!(re_ctx_p->flags & RE_FLAG_MULTILINE))
366354
{
367355
JERRY_DDLOG ("fail\n");
368-
re_ctx_p->recursion_depth--;
369356
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
370357
}
371358

@@ -376,7 +363,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
376363
}
377364

378365
JERRY_DDLOG ("fail\n");
379-
re_ctx_p->recursion_depth--;
380366
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
381367
}
382368
case RE_OP_ASSERT_END:
@@ -392,7 +378,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
392378
if (!(re_ctx_p->flags & RE_FLAG_MULTILINE))
393379
{
394380
JERRY_DDLOG ("fail\n");
395-
re_ctx_p->recursion_depth--;
396381
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
397382
}
398383

@@ -403,7 +388,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
403388
}
404389

405390
JERRY_DDLOG ("fail\n");
406-
re_ctx_p->recursion_depth--;
407391
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
408392
}
409393
case RE_OP_ASSERT_WORD_BOUNDARY:
@@ -435,7 +419,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
435419
if (is_wordchar_left == is_wordchar_right)
436420
{
437421
JERRY_DDLOG ("fail\n");
438-
re_ctx_p->recursion_depth--;
439422
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
440423
}
441424
}
@@ -447,7 +430,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
447430
if (is_wordchar_left != is_wordchar_right)
448431
{
449432
JERRY_DDLOG ("fail\n");
450-
re_ctx_p->recursion_depth--;
451433
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
452434
}
453435
}
@@ -502,8 +484,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
502484

503485
if (!ecma_is_completion_value_throw (match_value))
504486
{
505-
re_ctx_p->recursion_depth--;
506-
507487
if (ecma_is_value_true (match_value))
508488
{
509489
*out_iter_p = sub_iter;
@@ -529,7 +509,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
529509
if (lit_utf8_iterator_is_eos (&iter))
530510
{
531511
JERRY_DDLOG ("fail\n");
532-
re_ctx_p->recursion_depth--;
533512
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
534513
}
535514

@@ -559,7 +538,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
559538
if (!is_match)
560539
{
561540
JERRY_DDLOG ("fail\n");
562-
re_ctx_p->recursion_depth--;
563541
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
564542
}
565543
}
@@ -569,7 +547,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
569547
if (is_match)
570548
{
571549
JERRY_DDLOG ("fail\n");
572-
re_ctx_p->recursion_depth--;
573550
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
574551
}
575552
}
@@ -600,7 +577,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
600577
if ((iter.buf_p + iter.buf_pos.offset) >= re_ctx_p->input_end_p)
601578
{
602579
JERRY_DDLOG ("fail\n");
603-
re_ctx_p->recursion_depth--;
604580
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
605581
}
606582

@@ -610,7 +586,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
610586
if (ch1 != ch2)
611587
{
612588
JERRY_DDLOG ("fail\n");
613-
re_ctx_p->recursion_depth--;
614589
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
615590
}
616591
}
@@ -633,7 +608,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
633608
if (ecma_is_value_true (match_value))
634609
{
635610
*out_iter_p = sub_iter;
636-
re_ctx_p->recursion_depth--;
637611
return match_value; /* match */
638612
}
639613
else if (ecma_is_completion_value_throw (match_value))
@@ -647,15 +621,13 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
647621
bc_p = old_bc_p;
648622

649623
re_ctx_p->saved_p[RE_GLOBAL_START_IDX] = old_start_p;
650-
re_ctx_p->recursion_depth--;
651624
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
652625
}
653626
case RE_OP_SAVE_AND_MATCH:
654627
{
655628
JERRY_DDLOG ("End of pattern is reached: match\n");
656629
re_ctx_p->saved_p[RE_GLOBAL_END_IDX] = iter;
657630
*out_iter_p = iter;
658-
re_ctx_p->recursion_depth--;
659631
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_TRUE); /* match */
660632
}
661633
case RE_OP_ALTERNATIVE:
@@ -720,7 +692,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
720692
if (ecma_is_value_true (match_value))
721693
{
722694
*out_iter_p = sub_iter;
723-
re_ctx_p->recursion_depth--;
724695
return match_value; /* match */
725696
}
726697
else if (ecma_is_completion_value_throw (match_value))
@@ -778,7 +749,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
778749
if (ecma_is_value_true (match_value))
779750
{
780751
*out_iter_p = sub_iter;
781-
re_ctx_p->recursion_depth--;
782752
return match_value; /* match */
783753
}
784754
else if (ecma_is_completion_value_throw (match_value))
@@ -802,7 +772,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
802772
if (ecma_is_value_true (match_value))
803773
{
804774
*out_iter_p = sub_iter;
805-
re_ctx_p->recursion_depth--;
806775
return match_value; /* match */
807776
}
808777
else if (ecma_is_completion_value_throw (match_value))
@@ -812,7 +781,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
812781
}
813782

814783
re_ctx_p->saved_p[start_idx] = old_start;
815-
re_ctx_p->recursion_depth--;
816784
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
817785
}
818786
case RE_OP_CAPTURE_NON_GREEDY_GROUP_END:
@@ -857,7 +825,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
857825
if (ecma_is_value_true (match_value))
858826
{
859827
*out_iter_p = sub_iter;
860-
re_ctx_p->recursion_depth--;
861828
return match_value; /* match */
862829
}
863830
else if (ecma_is_completion_value_throw (match_value))
@@ -907,7 +874,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
907874
&& iter.buf_p == re_ctx_p->saved_p[start_idx].buf_p
908875
&& iter.buf_pos.offset == re_ctx_p->saved_p[start_idx].buf_pos.offset)
909876
{
910-
re_ctx_p->recursion_depth--;
911877
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
912878
}
913879

@@ -929,7 +895,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
929895
if (ecma_is_value_true (match_value))
930896
{
931897
*out_iter_p = sub_iter;
932-
re_ctx_p->recursion_depth--;
933898
return match_value; /* match */
934899
}
935900
else if (ecma_is_completion_value_throw (match_value))
@@ -954,7 +919,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
954919
if (ecma_is_value_true (match_value))
955920
{
956921
*out_iter_p = sub_iter;
957-
re_ctx_p->recursion_depth--;
958922
return match_value; /* match */
959923
}
960924
else if (ecma_is_completion_value_throw (match_value))
@@ -976,7 +940,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
976940
if (ecma_is_value_true (match_value))
977941
{
978942
*out_iter_p = sub_iter;
979-
re_ctx_p->recursion_depth--;
980943
return match_value; /* match */
981944
}
982945
else if (ecma_is_completion_value_throw (match_value))
@@ -988,7 +951,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
988951
/* restore if fails */
989952
re_ctx_p->saved_p[end_idx] = old_end;
990953
re_ctx_p->num_of_iterations_p[iter_idx]--;
991-
re_ctx_p->recursion_depth--;
992954
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
993955
}
994956
case RE_OP_NON_GREEDY_ITERATOR:
@@ -1013,7 +975,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
1013975
if (ecma_is_value_true (match_value))
1014976
{
1015977
*out_iter_p = sub_iter;
1016-
re_ctx_p->recursion_depth--;
1017978
return match_value; /* match */
1018979
}
1019980
else if (ecma_is_completion_value_throw (match_value))
@@ -1037,7 +998,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
1037998
iter = sub_iter;
1038999
num_of_iter++;
10391000
}
1040-
re_ctx_p->recursion_depth--;
10411001
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
10421002
}
10431003
case RE_OP_GREEDY_ITERATOR:
@@ -1079,7 +1039,6 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
10791039
if (ecma_is_value_true (match_value))
10801040
{
10811041
*out_iter_p = sub_iter;
1082-
re_ctx_p->recursion_depth--;
10831042
return match_value; /* match */
10841043
}
10851044
else if (ecma_is_completion_value_throw (match_value))
@@ -1095,13 +1054,11 @@ re_match_regexp (re_matcher_ctx_t *re_ctx_p, /**< RegExp matcher context */
10951054
lit_utf8_iterator_read_prev (&iter);
10961055
num_of_iter--;
10971056
}
1098-
re_ctx_p->recursion_depth--;
10991057
return ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_FALSE); /* fail */
11001058
}
11011059
default:
11021060
{
11031061
JERRY_DDLOG ("UNKNOWN opcode (%d)!\n", (uint32_t) op);
1104-
re_ctx_p->recursion_depth--;
11051062
return ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_COMMON));
11061063
}
11071064
}
@@ -1238,7 +1195,6 @@ ecma_regexp_exec_helper (ecma_value_t regexp_value, /**< RegExp object */
12381195
re_matcher_ctx_t re_ctx;
12391196
re_ctx.input_start_p = iterator.buf_p;
12401197
re_ctx.input_end_p = iterator.buf_p + iterator.buf_size;
1241-
re_ctx.recursion_depth = 0;
12421198

12431199
/* 1. Read bytecode header and init regexp matcher context. */
12441200
re_ctx.flags = (uint8_t) re_get_value (&bc_p);

jerry-core/ecma/operations/ecma-regexp-object.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@
2929
* @{
3030
*/
3131

32-
/**
33-
* Limit of RegExp executor recursion depth
34-
*/
35-
#define RE_EXECUTE_RECURSION_LIMIT 1000
36-
3732
/**
3833
* RegExp flags
3934
*/
@@ -49,7 +44,6 @@ typedef struct
4944
lit_utf8_iterator_t *saved_p; /**< saved result string pointers, ECMA 262 v5, 15.10.2.1, State */
5045
const lit_utf8_byte_t *input_start_p; /**< start of input pattern string */
5146
const lit_utf8_byte_t *input_end_p; /**< end of input pattern string */
52-
uint32_t recursion_depth; /**< recursion depth counter */
5347
uint32_t num_of_captures; /**< number of capture groups */
5448
uint32_t num_of_non_captures; /**< number of non-capture groups */
5549
uint32_t *num_of_iterations_p; /**< number of iterations */

jerry-core/parser/regexp/re-compiler.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,6 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
384384

385385
uint32_t alterantive_offset = re_get_bytecode_length (re_ctx_p->bytecode_ctx_p);
386386

387-
if (re_ctx_p->recursion_depth >= RE_COMPILE_RECURSION_LIMIT)
388-
{
389-
ret_value = ecma_raise_range_error ("RegExp compiler recursion limit is exceeded.");
390-
return ret_value;
391-
}
392-
re_ctx_p->recursion_depth++;
393-
394387
while (true)
395388
{
396389
ECMA_TRY_CATCH (empty,
@@ -604,7 +597,6 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
604597
else
605598
{
606599
re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset);
607-
re_ctx_p->recursion_depth--;
608600
}
609601

610602
return ret_value;
@@ -618,7 +610,6 @@ re_parse_alternative (re_compiler_ctx_t *re_ctx_p, /**< RegExp compiler context
618610
else
619611
{
620612
re_insert_u32 (bc_ctx_p, alterantive_offset, re_get_bytecode_length (bc_ctx_p) - alterantive_offset);
621-
re_ctx_p->recursion_depth--;
622613
}
623614

624615
return ret_value;
@@ -651,7 +642,6 @@ re_compile_bytecode (ecma_property_t *bytecode_p, /**< bytecode */
651642
re_ctx.flags = flags;
652643
re_ctx.highest_backref = 0;
653644
re_ctx.num_of_non_captures = 0;
654-
re_ctx.recursion_depth = 0;
655645

656646
re_bytecode_ctx_t bc_ctx;
657647
bc_ctx.block_start_p = NULL;

jerry-core/parser/regexp/re-compiler.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ typedef enum
6161
RE_OP_INV_CHAR_CLASS
6262
} re_opcode_t;
6363

64-
/**
65-
* Recursion limit of RegExp compiler
66-
*/
67-
#define RE_COMPILE_RECURSION_LIMIT 100
68-
6964
/**
7065
* Check if a RegExp opcode is a capture group or not
7166
*/
@@ -92,7 +87,6 @@ typedef struct
9287
typedef struct
9388
{
9489
uint8_t flags; /**< RegExp flags */
95-
uint32_t recursion_depth; /**< recursion depth */
9690
uint32_t num_of_captures; /**< number of capture groups */
9791
uint32_t num_of_non_captures; /**< number of non-capture groups */
9892
uint32_t highest_backref; /**< highest backreference */

0 commit comments

Comments
 (0)