Commit 21965276

21965276c9f6Merged

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

Description

fix(regexp): raise RE_MAX_CAPTURES to 4096 (#820, #776)

((((...((a))...)))) with depth 500 used to raise RegexpError "too many capture groups" because RE_MAX_CAPTURES was arbitrarily set to 32. CRuby handles arbitrarily deep nesting; raising the cap to 4096 covers 1000+ depth with room to spare.

The runtime memory allocation scales with the actual per-regex ncap (see re_exec.c comment) so this is purely a compile-time cap -- larger limit doesn't bloat every regex.

Closes #820 Closes #776

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

Details

Commit
21965276c9f6
Parent
688dda3686a6

lib/regexp/re_internal.hmodifiedView file

@@ -109,8 +109,12 @@
109109 #define MRB_REGEXP_STEP_LIMIT 1000000
110110 #endif
111111
112-/* Maximum captures */
113-#define RE_MAX_CAPTURES 32
112+/* Maximum captures. Issues #820, #776: raised from 32 so deeply
113+ nested-paren regexes (`((((...((a))...))))`) compile instead of
114+ raising RegexpError. 4096 covers >1k-deep nesting with room to
115+ spare; runtime memory scales with actual ncap per regex (see
116+ re_exec.c comment) so this is purely a compile-time cap. */
117+#define RE_MAX_CAPTURES 4096
114118
115119 /* Thread struct for Pike VM. `sp` is the input position the thread is
116120 waiting for; the outer loop only dispatches a thread when its sp