Commit ab823852

ab8238525325Merged

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

Description

Fix clang-exposed UB in regexp errors and File separator literals

Two latent bugs that gcc happened to tolerate but clang's codegen/layout exposed (CI red, also caught by UBSan):

  • sp_re_default_error_handler passed the regex compiler's stack buffer straight to sp_raise_cls, which stores the pointer and longjmps past that frame -- e.message then read freed stack ("untermin"). Copy the message to a GC string first, mirroring the startup handler.

  • File::SEPARATOR / PATH_SEPARATOR were emitted as bare "/" / ":" literals, not the marker-framed (&("\xff" "/")[1]) form every spinel string uses. sp_str_byte_len/sp_str_concat then read literal[-1] out of bounds (crash under clang). Frame them like ALT_SEPARATOR.

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

Details

Commit
ab8238525325
Parent
f607138b4f61

lib/sp_runtime.hmodifiedView file

@@ -4540,6 +4540,18 @@
45404540 library calls sp_re_set_error_handler at startup -- codegen emits
45414541 the install call after the exception infrastructure is set up. */
45424542 static void sp_re_default_error_handler(const char *msg) {
4543+ /* msg points at the regex compiler's stack buffer. sp_raise_cls stores
4544+ the pointer and longjmps past that frame, leaving it dangling -- copy
4545+ to a GC-managed string first (mirrors sp_re_startup_error_handler).
4546+ gcc happened to leave the stack intact; clang reused it, so e.message
4547+ read garbage (regexp_error_catchable). */
4548+ if (msg) {
4549+ size_t n = strlen(msg);
4550+ char *buf = sp_str_alloc_raw(n + 1);
4551+ memcpy(buf, msg, n);
4552+ buf[n] = 0;
4553+ msg = buf;
4554+ }
45434555 sp_raise_cls("RegexpError", msg);
45444556 }
45454557 /* Issue #846: during sp_re_init (before main()'s setjmp scope is