Commit 080b4521

080b45215710Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · ~2 modified

Description

regexp: cap bt_match recursion depth to fail gracefully

bt_match recurses on SPLIT / SAVE / lookahead / lookbehind. Without a ceiling, pathological patterns (long alternation chains or backref plus many quantifier iterations) could drive the C stack to overflow rather than producing a clean "no match".

Wrap the recursion in bt_match_depth and bail with FALSE once depth exceeds MRB_REGEXP_DEPTH_LIMIT (10000 frames, well under a default 8 MB stack). Conservative -- treats over-deep paths as failed match, matching how the existing steps-limit guard behaves.

The C stack itself stays the recursion substrate; the user explicitly opted against a full explicit-stack rewrite when an early-fail check is enough.

Fixes #777

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
080b45215710
Parent
a02bb3d902dd

lib/regexp/re_exec.cmodifiedView file

@@ -430,9 +430,27 @@
430430 * Step-limited to prevent ReDoS.
431431 */
432432 static mrb_bool
433+bt_match_depth(const mrb_regexp_pattern *pat, const char *str, const char *str_end,
434+ const char *sp, uint32_t pc, int *captures, int ncap, int *steps,
435+ int depth);
436+
437+static mrb_bool
433438 bt_match(const mrb_regexp_pattern *pat, const char *str, const char *str_end,
434439 const char *sp, uint32_t pc, int *captures, int ncap, int *steps)
435440 {
441+ return bt_match_depth(pat, str, str_end, sp, pc, captures, ncap, steps, 0);
442+}
443+
444+static mrb_bool
445+bt_match_depth(const mrb_regexp_pattern *pat, const char *str, const char *str_end,
446+ const char *sp, uint32_t pc, int *captures, int ncap, int *steps,
447+ int depth)
448+{
449+ /* Issue #777: bail before driving the C stack to overflow. The
450+ callers below recurse on SPLIT / SAVE / lookahead / lookbehind;
451+ each level adds one C frame. Treat over-deep as a failed match
452+ -- conservative, in line with how steps-limit is handled. */
453+ if (depth > MRB_REGEXP_DEPTH_LIMIT) return FALSE;
436454 while (pc < pat->code_len) {
437455 if (++(*steps) > MRB_REGEXP_STEP_LIMIT) return FALSE;
438456
@@ -483,12 +501,12 @@
483501 break;
484502
485503 case RE_SPLIT:
486- if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) return TRUE;
504+ if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return TRUE;
487505 pc = inst.offset;
488506 break;
489507
490508 case RE_SPLITNG:
491- if (bt_match(pat, str, str_end, sp, inst.offset, captures, ncap, steps)) return TRUE;
509+ if (bt_match_depth(pat, str, str_end, sp, inst.offset, captures, ncap, steps, depth + 1)) return TRUE;
492510 pc++;
493511 break;
494512
@@ -498,7 +516,7 @@
498516 if (slot < ncap) {
499517 int old = captures[slot];
500518 captures[slot] = (int)(sp - str);
501- if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) return TRUE;
519+ if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return TRUE;
502520 captures[slot] = old;
503521 }
504522 return FALSE;
@@ -563,13 +581,13 @@
563581 break;
564582
565583 case RE_LOOKAHEAD:
566- if (!bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps))
584+ if (!bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1))
567585 return FALSE;
568586 pc = inst.offset;
569587 break;
570588
571589 case RE_NEG_LOOKAHEAD:
572- if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps))
590+ if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1))
573591 return FALSE;
574592 pc = inst.offset;
575593 break;
@@ -578,7 +596,7 @@
578596 {
579597 int lb_len = inst.a;
580598 if (sp - str < lb_len) return FALSE; /* not enough text before */
581- if (!bt_match(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps))
599+ if (!bt_match_depth(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps, depth + 1))
582600 return FALSE;
583601 pc = inst.offset;
584602 }
@@ -588,7 +606,7 @@
588606 {
589607 int lb_len = inst.a;
590608 if (sp - str >= lb_len) {
591- if (bt_match(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps))
609+ if (bt_match_depth(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps, depth + 1))
592610 return FALSE;
593611 }
594612 /* if not enough text before, negative lookbehind succeeds */