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
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "Regexp")) { int tp = ++g_tmp, ts = ++g_tmp; int flags = (argc >= 2) ? 1 : 0; /* See the Regexp.new path: emit the pattern into a local buffer so an interpolated arg's embedded-call arg roots land in g_pre as whole statements before this temp's decl, not inside its initializer. */ Buf pv; memset(&pv, 0, sizeof pv); emit_expr(c, argv[0], &pv); emit_indent(g_pre, g_indent); buf_printf(g_pre, "const char *_t%d = ", ts); emit_expr(c, argv[0], g_pre); buf_puts(g_pre, ";\n"); buf_printf(g_pre, "const char *_t%d = %s;\n", ts, pv.p ? pv.p : "\"\""); free(pv.p); emit_indent(g_pre, g_indent); buf_printf(g_pre, "mrb_regexp_pattern *_t%d = re_compile(_t%d, (int64_t)strlen(_t%d ? _t%d : \"\"), %d);\n", tp, ts, ts, ts, flags); if (cn && !strcmp(cn, "Regexp") && argc >= 1) { int tp = ++g_tmp, ts = ++g_tmp; int flags = (argc >= 2) ? 1 : 0; /* Regexp::IGNORECASE=1 if 2nd arg truthy */ /* Emit the pattern value into a local buffer first: an interpolated arg whose embedded call roots its own args pushes those decls to g_pre, which must land as whole statements BEFORE this temp's decl line, not inside its initializer. */ Buf pv; memset(&pv, 0, sizeof pv); emit_expr(c, argv[0], &pv); emit_indent(g_pre, g_indent); buf_printf(g_pre, "const char *_t%d = ", ts); emit_expr(c, argv[0], g_pre); buf_puts(g_pre, ";\n"); buf_printf(g_pre, "const char *_t%d = %s;\n", ts, pv.p ? pv.p : "\"\""); free(pv.p); emit_indent(g_pre, g_indent); buf_printf(g_pre, "mrb_regexp_pattern *_t%d = re_compile(_t%d, (int64_t)strlen(_t%d ? _t%d : \"\"), %d);\n", tp, ts, ts, ts, flags);src/codegen_util.cmodifiedView file
Loading diff…
test/interp_call_arg_root.rbaddedView file
Loading diff…
test/interp_call_arg_root.rb.expectedaddedView file
Loading diff…