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 10000
119119 #endif
120120
121-/* Maximum captures. Issues #820, #776: raised from 32 so deeply
122- nested-paren regexes (`((((...((a))...))))`) compile instead of
123- raising RegexpError. 4096 covers >1k-deep nesting with room to
124- spare; runtime memory scales with actual ncap per regex (see
121+/* Maximum captures. Sized for realistic code: complex parsers rarely
122+ exceed ~50-100 capture groups. Pathological inputs like depth-500
123+ `((((...((a))...))))` raise RegexpError, which is the same
124+ "fail-gracefully on absurd input" stance as MRB_REGEXP_DEPTH_LIMIT
125+ above. Runtime memory scales with actual ncap per regex (see
125126 re_exec.c comment) so this is purely a compile-time cap. */
126-#define RE_MAX_CAPTURES 4096
127+#define RE_MAX_CAPTURES 128
127128
128129 /* Thread struct for Pike VM. `sp` is the input position the thread is
129130 waiting for; the outer loop only dispatches a thread when its sp