Commit 8507ff7c

8507ff7c7e88Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · ~4 modified

Description

fix(regexp,runtime,codegen): extern error handler hook routes regex compile errors to sp_raise_cls (#781)

Previously compile_error called fprintf + exit(1), making any bad pattern fatal even when the caller wrapped Regexp.new in rescue. The blocker was linkage: sp_raise_cls is static inline per translation unit, so the library .a can't reference the user program's copy directly.

Three-piece bridge:

  • lib/regexp/re_compile.c: a function-pointer hook (sp_re_error_handler) that compile_error calls before falling back to exit. Public setter: sp_re_set_error_handler(fn).
  • lib/sp_runtime.h: a static-inline sp_re_default_error_handler that calls sp_raise_cls("RegexpError", msg). extern declaration for sp_re_set_error_handler.
  • spinel_codegen.rb: when @needs_regexp is on, emit the sp_re_set_error_handler(sp_re_default_error_handler) call at the top of main, before sp_re_init runs.

After this, runtime regex compilation errors (Regexp.new(var) patterns) raise RegexpError and are catchable by rescue. The literal-pattern hoist (sp_re_init at startup) still runs outside any rescue context, so a bad literal at toplevel prints "unhandled exception: ..." and exits cleanly -- much better than the previous raw fprintf + exit.

The literal-in-rescue hoist mismatch (where a begin/rescue around Regexp.new("*") doesn't catch because the compile was hoisted) is a separate codegen-level design choice -- the optimization shouldn't cross rescue boundaries, but fixing it needs analyze- side scope detection.

Closes #781

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

Details

Commit
8507ff7c7e88
Parent
21965276c9f6

lib/regexp/re_compile.cmodifiedView file

@@ -33,18 +33,31 @@
3333
3434 static void compile_alt(re_compiler *c); /* forward */
3535
36-/* Issue #781: kept exit(1) for now -- routing through sp_raise_cls
37- would require a non-static linkage hook (the runtime's
38- sp_raise_cls is `static inline` per translation unit, so the
39- library cannot reference the user program's copy directly). A
40- later commit can introduce an extern raise hook + populate it
41- from the user program. */
36+/* Issue #781: error handler hook so the library can route through
37+ the user program's sp_raise_cls (which is `static inline` per
38+ translation unit and not directly linkable from a .a). The user
39+ program calls sp_re_set_error_handler(fn) at startup; fn should
40+ not return (typically wraps sp_raise_cls). If unset, fall back
41+ to fprintf + exit. */
42+static void (*sp_re_error_handler)(const char *msg) = NULL;
43+void sp_re_set_error_handler(void (*fn)(const char *msg)) {
44+ sp_re_error_handler = fn;
45+}
46+
4247 static void
4348 compile_error(re_compiler *c, const char *msg)
4449 {
4550 if (c->stripped) free(c->stripped);
4651 c->stripped = NULL;
47- fprintf(stderr, "RegexpError: %s: /%.*s/\n", msg, (int)(c->src_end - c->src), c->src); exit(1);
52+ char buf[1024];
53+ snprintf(buf, sizeof(buf), "%s: /%.*s/",
54+ msg, (int)(c->src_end - c->src), c->src);
55+ if (sp_re_error_handler) {
56+ sp_re_error_handler(buf);
57+ /* shouldn't return; fall through to exit as a safety net */
58+ }
59+ fprintf(stderr, "RegexpError: %s\n", buf);
60+ exit(1);
4861 }
4962
5063 static uint32_t