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
* Step-limited to prevent ReDoS. */static mrb_boolbt_match_depth(const mrb_regexp_pattern *pat, const char *str, const char *str_end, const char *sp, uint32_t pc, int *captures, int ncap, int *steps, int depth);static mrb_boolbt_match(const mrb_regexp_pattern *pat, const char *str, const char *str_end, const char *sp, uint32_t pc, int *captures, int ncap, int *steps){ return bt_match_depth(pat, str, str_end, sp, pc, captures, ncap, steps, 0);}static mrb_boolbt_match_depth(const mrb_regexp_pattern *pat, const char *str, const char *str_end, const char *sp, uint32_t pc, int *captures, int ncap, int *steps, int depth){ /* Issue #777: bail before driving the C stack to overflow. The callers below recurse on SPLIT / SAVE / lookahead / lookbehind; each level adds one C frame. Treat over-deep as a failed match -- conservative, in line with how steps-limit is handled. */ if (depth > MRB_REGEXP_DEPTH_LIMIT) return FALSE; while (pc < pat->code_len) { if (++(*steps) > MRB_REGEXP_STEP_LIMIT) return FALSE; break; case RE_SPLIT: if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) return TRUE; if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return TRUE; pc = inst.offset; break; case RE_SPLITNG: if (bt_match(pat, str, str_end, sp, inst.offset, captures, ncap, steps)) return TRUE; if (bt_match_depth(pat, str, str_end, sp, inst.offset, captures, ncap, steps, depth + 1)) return TRUE; pc++; break; if (slot < ncap) { int old = captures[slot]; captures[slot] = (int)(sp - str); if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) return TRUE; if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return TRUE; captures[slot] = old; } return FALSE; break; case RE_LOOKAHEAD: if (!bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) if (!bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return FALSE; pc = inst.offset; break; case RE_NEG_LOOKAHEAD: if (bt_match(pat, str, str_end, sp, pc + 1, captures, ncap, steps)) if (bt_match_depth(pat, str, str_end, sp, pc + 1, captures, ncap, steps, depth + 1)) return FALSE; pc = inst.offset; break; { int lb_len = inst.a; if (sp - str < lb_len) return FALSE; /* not enough text before */ if (!bt_match(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps)) if (!bt_match_depth(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps, depth + 1)) return FALSE; pc = inst.offset; } { int lb_len = inst.a; if (sp - str >= lb_len) { if (bt_match(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps)) if (bt_match_depth(pat, str, str_end, sp - lb_len, pc + 1, captures, ncap, steps, depth + 1)) return FALSE; } /* if not enough text before, negative lookbehind succeeds */lib/regexp/re_internal.hmodifiedView file
Loading diff…