Commit 53a523ba
53a523ba0225Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
runtime,codegen: Regexp.new error catchable via stash-and-defer
Regexp.new("[invalid") used to terminate the process: spinel
folds literal-arg Regexp.new into the static pattern table and
pre-compiles in sp_re_init (called before main()'s setjmp
scope). The library's compile_error -> sp_re_error_handler ->
sp_raise_cls path then hit sp_exc_top == 0 and went straight to
fprintf + exit.
Install a quiet startup handler during sp_re_init that longjmps back to a wrapping setjmp around each per-slot re_compile. Capture the message into sp_re_pat_err_; the slot itself is left NULL. regex_pat_c_expr checks the per-slot err first and raises a catchable RegexpError from inside the user's begin/ rescue scope. After sp_re_init the default raising handler is restored so dyn-compile errors stay catchable too.
Refs #846
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 53a523ba0225
- Parent
- eec56fd026a0
lib/sp_runtime.hmodifiedView file
static void sp_re_default_error_handler(const char *msg) { sp_raise_cls("RegexpError", msg);}/* Issue #846: during sp_re_init (before main()'s setjmp scope is active), a bad literal `Regexp.new("[invalid")` pattern would route through sp_raise_cls -> "unhandled exception" + exit because sp_exc_top is 0. Install a startup handler that longjmps back to sp_re_init's wrapping setjmp; the codegen-emitted loop then stashes the error per slot for a deferred raise from the first use site (where the user's begin/rescue is active). The re_compile contract requires the error callback to not return (otherwise the library's fall-through fprintf+exit fires) so we longjmp out. */static const char *sp_re_startup_err = NULL;static jmp_buf sp_re_startup_jmp;static void sp_re_startup_error_handler(const char *msg) { if (msg) { size_t n = strlen(msg); char *buf = sp_str_alloc_raw(n + 1); memcpy(buf, msg, n); buf[n] = 0; sp_re_startup_err = buf; } longjmp(sp_re_startup_jmp, 1);}extern void sp_re_set_error_handler(void (*fn)(const char *msg));static void sp_mark_in_flight_exceptions(void) { for (int i = 0; i < sp_exc_top; i++) sp_mark_string(sp_exc_msg[i]); }spinel_codegen.rbmodifiedView file
Loading diff…
test/regexp_error_catchable.rbaddedView file
Loading diff…
test/regexp_error_catchable.rb.expectedaddedView file
Loading diff…