Commit 1c9a79c2

1c9a79c2781fMerged

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

Description

fix(codegen,runtime): String#gsub(regex, hash) routes to hash-aware helper

Closes #485. Spinel's String#gsub codegen path emitted a call to sp_re_gsub(pat, str, const char *rep) regardless of the second arg's type. When the second arg was a Hash (Ruby's per-match hash-lookup form, common in HTML / JSON escape idioms), the hash pointer was smuggled through where rep was expected and the binary read garbage from the pointer's memory.

Runtime: new sp_re_gsub_str_str_hash helper that walks matches the same way sp_re_gsub does but per-match looks the matched substring up in the StrStrHash. Missing keys lower to "" to match CRuby's s.gsub(re, hash) semantics.

Codegen: at the gsub(regex, ...) call-site dispatch, peek at the second arg's static type. If it's str_str_hash, emit the hash-aware helper; otherwise keep the existing const char * emit. Scope: str_str_hash only — sym_str_hash and the poly variants are rarer; can extend the dispatch table as they show up.

Reproduces the issue's repro: ESCAPES = {"&" => "&", ...} puts "a&b<c".gsub(/[&<>]/, ESCAPES) now prints a&amp;b&lt;c (matches CRuby), was garbage.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
1c9a79c2781f
Parent
1a91d107fe2e

lib/sp_runtime.hmodifiedView file

@@ -973,6 +973,38 @@
973973 out[olen] = 0; return out;
974974 }
975975
976+/* String#gsub(regex, hash) — per-match hash lookup form. CRuby's
977+ * semantics: each matched substring is looked up as a key in the
978+ * hash; the value (if present) is the replacement, otherwise the
979+ * matched substring is dropped (CRuby returns "", not the match).
980+ * Used by html_escape / json_escape idioms (gsub(/[&<>]/, ESCAPES)). */
981+static const char *sp_re_gsub_str_str_hash(mrb_regexp_pattern *pat, const char *str, sp_StrStrHash *h) {
982+ int64_t slen = (int64_t)strlen(str);
983+ size_t cap = slen * 2 + 64; char *out = sp_str_alloc_raw(cap); size_t olen = 0;
984+ int64_t pos = 0; int caps[64];
985+ while (pos <= slen) {
986+ int n = re_exec(pat, str, slen, pos, caps, 64);
987+ if (n <= 0 || caps[0] < 0) break;
988+ size_t before = caps[0] - pos;
989+ int mlen = caps[1] - caps[0];
990+ char keybuf[64];
991+ char *key = mlen < (int)sizeof(keybuf) ? keybuf : (char *)malloc(mlen + 1);
992+ memcpy(key, str + caps[0], mlen);
993+ key[mlen] = 0;
994+ const char *rep = sp_StrStrHash_has_key(h, key) ? sp_StrStrHash_get(h, key) : "";
995+ size_t rlen = strlen(rep);
996+ if (olen + before + rlen >= cap) { cap = (olen + before + rlen) * 2 + 64; out = (char *)realloc(out, cap); }
997+ memcpy(out + olen, str + pos, before); olen += before;
998+ memcpy(out + olen, rep, rlen); olen += rlen;
999+ if (key != keybuf) free(key);
1000+ pos = caps[1]; if (caps[0] == caps[1]) pos++;
1001+ }
1002+ size_t rest = slen - pos;
1003+ if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); }
1004+ memcpy(out + olen, str + pos, rest); olen += rest;
1005+ out[olen] = 0; return out;
1006+}
1007+
9761008 static const char *sp_re_sub(mrb_regexp_pattern *pat, const char *str, const char *rep) {
9771009 int64_t slen = (int64_t)strlen(str); size_t rlen = strlen(rep);
9781010 int caps[64];