Commit 4d8424e9

4d8424e97395Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Fix illegal C from an embedded call's arg root in a regexp pattern (#1498)

Regexp.new / Regexp.compile emitted the pattern temp as buf_printf(g_pre, "const char *_tN = "); emit_expr(argv[0], g_pre) -- writing the declaration prefix to g_pre and then emitting the argument straight into g_pre too. When that argument is a string interpolation whose embedded call roots its own string-literal arg, the call's rooting decl (const char *_t5 = ...; SP_GC_ROOT(_t5);) also goes to g_pre and landed inside the half-written const char *_t3 = line:

const char *_t3 =   const char *_t5 = (...);   // a decl in init position
SP_GC_ROOT(_t5);
({ ...; sp_sprintf(...); });

cc then rejected "expected expression before 'const'" and _t5 leaked out of scope. Emit the pattern value into a local buffer first (as the format/sprintf path already does), so the embedded preludes flush to g_pre as whole statements before the temp's declaration. Same fix for the interpolated regex-literal path in emit_regex_pat_to_buf.

Closes #1498.

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

Details

Commit
4d8424e97395
Parent
10d21739ecb2

src/codegen_call.cmodifiedView file

@@ -4791,8 +4791,14 @@
47914791 nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Regexp")) {
47924792 int tp = ++g_tmp, ts = ++g_tmp;
47934793 int flags = (argc >= 2) ? 1 : 0;
4794+ /* See the Regexp.new path: emit the pattern into a local buffer so an
4795+ interpolated arg's embedded-call arg roots land in g_pre as whole
4796+ statements before this temp's decl, not inside its initializer. */
4797+ Buf pv; memset(&pv, 0, sizeof pv);
4798+ emit_expr(c, argv[0], &pv);
47944799 emit_indent(g_pre, g_indent);
4795- buf_printf(g_pre, "const char *_t%d = ", ts); emit_expr(c, argv[0], g_pre); buf_puts(g_pre, ";\n");
4800+ buf_printf(g_pre, "const char *_t%d = %s;\n", ts, pv.p ? pv.p : "\"\"");
4801+ free(pv.p);
47964802 emit_indent(g_pre, g_indent);
47974803 buf_printf(g_pre, "mrb_regexp_pattern *_t%d = re_compile(_t%d, (int64_t)strlen(_t%d ? _t%d : \"\"), %d);\n",
47984804 tp, ts, ts, ts, flags);
@@ -5737,8 +5743,15 @@
57375743 if (cn && !strcmp(cn, "Regexp") && argc >= 1) {
57385744 int tp = ++g_tmp, ts = ++g_tmp;
57395745 int flags = (argc >= 2) ? 1 : 0; /* Regexp::IGNORECASE=1 if 2nd arg truthy */
5746+ /* Emit the pattern value into a local buffer first: an interpolated arg
5747+ whose embedded call roots its own args pushes those decls to g_pre,
5748+ which must land as whole statements BEFORE this temp's decl line, not
5749+ inside its initializer. */
5750+ Buf pv; memset(&pv, 0, sizeof pv);
5751+ emit_expr(c, argv[0], &pv);
57405752 emit_indent(g_pre, g_indent);
5741- buf_printf(g_pre, "const char *_t%d = ", ts); emit_expr(c, argv[0], g_pre); buf_puts(g_pre, ";\n");
5753+ buf_printf(g_pre, "const char *_t%d = %s;\n", ts, pv.p ? pv.p : "\"\"");
5754+ free(pv.p);
57425755 emit_indent(g_pre, g_indent);
57435756 buf_printf(g_pre, "mrb_regexp_pattern *_t%d = re_compile(_t%d, (int64_t)strlen(_t%d ? _t%d : \"\"), %d);\n",
57445757 tp, ts, ts, ts, flags);