Commit 7d364cf2
7d364cf24d53Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
regexp: lower RE_MAX_CAPTURES to 128
4096 was set to support pathological depth-500 nested-paren regexes, but those don't appear in real code. Match the fail-gracefully stance already used for bt_match recursion (MRB_REGEXP_DEPTH_LIMIT): allow realistic capture counts, raise RegexpError on absurd input.
128 comfortably covers complex parsers (~50-100 groups is the high end in practice). Runtime memory is unaffected since allocation scales with actual ncap, not RE_MAX_CAPTURES.
Refs #776 #820
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 7d364cf24d53
- Parent
- 1fe58ef621f5
lib/regexp/re_internal.hmodifiedView file
@@ -118,12 +118,13 @@
118118
#define MRB_REGEXP_DEPTH_LIMIT 10000119119
#endif120120
121-
/* Maximum captures. Issues #820, #776: raised from 32 so deeply122-
nested-paren regexes (`((((...((a))...))))`) compile instead of123-
raising RegexpError. 4096 covers >1k-deep nesting with room to124-
spare; runtime memory scales with actual ncap per regex (see121+
/* Maximum captures. Sized for realistic code: complex parsers rarely122+
exceed ~50-100 capture groups. Pathological inputs like depth-500123+
`((((...((a))...))))` raise RegexpError, which is the same124+
"fail-gracefully on absurd input" stance as MRB_REGEXP_DEPTH_LIMIT125+
above. Runtime memory scales with actual ncap per regex (see125126
re_exec.c comment) so this is purely a compile-time cap. */126-
#define RE_MAX_CAPTURES 4096127+
#define RE_MAX_CAPTURES 128127128
128129
/* Thread struct for Pike VM. `sp` is the input position the thread is129130
waiting for; the outer loop only dispatches a thread when its sp