From 6edef4e7e6181c651cc552f101cd0887a0fbd8ca Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 23 Mar 2026 09:37:31 +0900 Subject: [PATCH] mruby-regexp: use dynamic captures allocation in exec_match() Replace fixed RE_MAX_CAPTURES*2 (256 bytes) stack array with malloc sized to actual pat->num_captures*2. Consistent with the Pike VM's dynamic ncap-sized pool. Co-authored-by: Claude --- mrbgems/mruby-regexp/src/regexp.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mrbgems/mruby-regexp/src/regexp.c b/mrbgems/mruby-regexp/src/regexp.c index 0d13ef5bd..c4216c88b 100644 --- a/mrbgems/mruby-regexp/src/regexp.c +++ b/mrbgems/mruby-regexp/src/regexp.c @@ -196,16 +196,20 @@ exec_match(mrb_state *mrb, mrb_value self, mrb_value str, mrb_int pos) mrb_regexp_pattern *pat = DATA_GET_PTR(mrb, self, ®exp_type, mrb_regexp_pattern); if (!pat) mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized Regexp"); - int captures[RE_MAX_CAPTURES * 2]; - memset(captures, -1, sizeof(captures)); + int cap_size = pat->num_captures * 2; + int *captures = (int*)mrb_malloc(mrb, sizeof(int) * cap_size); + memset(captures, -1, sizeof(int) * cap_size); int ncap = re_exec(mrb, pat, RSTRING_PTR(str), RSTRING_LEN(str), pos, - captures, pat->num_captures * 2); + captures, cap_size); if (ncap == 0) { + mrb_free(mrb, captures); clear_match_globals(mrb); return mrb_nil_value(); } - return create_matchdata(mrb, self, str, captures, pat->num_captures * 2); + mrb_value md = create_matchdata(mrb, self, str, captures, cap_size); + mrb_free(mrb, captures); + return md; } /*