Commit efe9e83d

efe9e83dcc1eMerged

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

Description

Build regex sub/gsub/scrub into a malloc buffer, not a sp_str body

sp_re_gsub, sp_re_sub, sp_re_gsub_str_str_hash and sp_str_scrub built their result into a buffer from sp_str_alloc_raw and grew it with realloc (directly and inside sp_re_expand_rep). That pointer points past the string header, so it is not a malloc base: once the output exceeded the initial capacity, realloc ran on an invalid pointer -- undefined behaviour that corrupted the heap and crashed. It stayed latent because the initial cap (slen*2 + slack) covers every ordinary case, so the realloc path is cold; ("a"*50).gsub(/a/, "zzzz"), ("x"*100).sub(/x+/, '\0\0'), the gsub(regex, hash) form, and ("\xFF"*100).scrub("XXXXX") all segfaulted.

Build into a plain malloc scratch buffer (realloc-safe), then emit a string sized to exactly the bytes written via sp_str_alloc, mirroring sp_str_gsub. This also subsumes the earlier sp_str_set_len length fix for these functions, since sp_str_alloc sets the length itself. Both sp_re_expand_rep callers (gsub and sub) convert together so the shared helper only ever reallocs a real malloc base. The exact-allocation variants (sp_re_sub_str_str_hash, sp_str_sub_str_str_hash) never realloc and are unchanged.

Refs #1308

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

Details

Commit
efe9e83dcc1e
Parent
ec7cd2249fdc

lib/sp_runtime.hmodifiedView file

@@ -1691,7 +1691,9 @@
16911691 size_t rlen = strlen(r);
16921692 size_t bl = sp_str_byte_len(s);
16931693 size_t cap = bl + 64;
1694- char *out = sp_str_alloc_raw(cap);
1694+ /* malloc scratch (grown with realloc on invalid-byte runs); the final
1695+ string is emitted at the exact length below. */
1696+ char *out = (char *)malloc(cap);
16951697 size_t olen = 0;
16961698 size_t i = 0;
16971699 while (i < bl) {
@@ -1720,9 +1722,10 @@
17201722 i += 1;
17211723 }
17221724 }
1723- out[olen] = 0;
1724- sp_str_set_len(out, olen);
1725- return out;
1725+ char *res = sp_str_alloc(olen);
1726+ memcpy(res, out, olen);
1727+ free(out);
1728+ return res;
17261729 }
17271730 static const char*sp_str_ljust(const char*s,mrb_int w){if(!s)return sp_str_empty;mrb_int cl=sp_str_length(s);if(cl>=w)return s;size_t bl=strlen(s);size_t pad=(size_t)(w-cl);char*r=sp_str_alloc_raw(bl+pad+1);memcpy(r,s,bl);memset(r+bl,' ',pad);r[bl+pad]=0;return r;}
17281731 static const char*sp_str_rjust(const char*s,mrb_int w){if(!s)return sp_str_empty;mrb_int cl=sp_str_length(s);if(cl>=w)return s;size_t bl=strlen(s);size_t pad=(size_t)(w-cl);char*r=sp_str_alloc_raw(bl+pad+1);memset(r,' ',pad);memcpy(r+pad,s,bl);r[bl+pad]=0;return r;}
@@ -2218,7 +2221,11 @@
22182221 static const char *sp_re_gsub(mrb_regexp_pattern *pat, const char *str, const char *rep) {
22192222 int64_t slen = (int64_t)strlen(str); size_t rlen = strlen(rep);
22202223 size_t cap = slen * 2 + rlen * 4 + 64;
2221- char *out = sp_str_alloc_raw(cap); size_t olen = 0;
2224+ /* Build into a plain malloc scratch: the buffer is grown with realloc
2225+ here and inside sp_re_expand_rep, which is only valid on a real
2226+ malloc base (a sp_str body pointer is offset past its header). The
2227+ final string is allocated at the exact length below. */
2228+ char *out = (char *)malloc(cap); size_t olen = 0;
22222229 int64_t pos = 0; int caps[64];
22232230 while (pos <= slen) {
22242231 int n = re_exec(pat, str, slen, pos, caps, 64);
@@ -2248,10 +2255,12 @@
22482255 if (olen+rest+1 >= cap) { cap = olen+rest+64; out = (char*)realloc(out, cap); }
22492256 memcpy(out+olen, str+pos, rest); olen += rest;
22502257 }
2251- /* The buffer is over-allocated (cap has slack); set the string's
2252- stored length to the bytes actually written so #length / inspect
2253- don't read the trailing slack. */
2254- out[olen] = 0; sp_str_set_len(out, olen); return out;
2258+ /* Emit a string sized to exactly the bytes written (sp_str_alloc sets
2259+ the length and null-terminates); release the scratch. */
2260+ char *res = sp_str_alloc(olen);
2261+ memcpy(res, out, olen);
2262+ free(out);
2263+ return res;
22552264 }
22562265
22572266 /* String#gsub(regex, hash) — per-match hash lookup form. CRuby's
@@ -2261,7 +2270,8 @@
22612270 * Used by html_escape / json_escape idioms (gsub(/[&<>]/, ESCAPES)). */
22622271 static const char *sp_re_gsub_str_str_hash(mrb_regexp_pattern *pat, const char *str, sp_StrStrHash *h) {
22632272 int64_t slen = (int64_t)strlen(str);
2264- size_t cap = slen * 2 + 64; char *out = sp_str_alloc_raw(cap); size_t olen = 0;
2273+ /* malloc scratch (realloc-safe); exact-sized string emitted below. */
2274+ size_t cap = slen * 2 + 64; char *out = (char *)malloc(cap); size_t olen = 0;
22652275 int64_t pos = 0; int caps[64];
22662276 while (pos <= slen) {
22672277 int n = re_exec(pat, str, slen, pos, caps, 64);
@@ -2295,7 +2305,10 @@
22952305 if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); }
22962306 memcpy(out + olen, str + pos, rest); olen += rest;
22972307 }
2298- out[olen] = 0; sp_str_set_len(out, olen); return out;
2308+ char *res = sp_str_alloc(olen);
2309+ memcpy(res, out, olen);
2310+ free(out);
2311+ return res;
22992312 }
23002313
23012314 /* Issue #910: sub(regex, hash) — same lookup semantics as
@@ -2350,16 +2363,19 @@
23502363 if (n <= 0 || caps[0] < 0) return str;
23512364 /* Issue #855: expand `\1`..`\9` / `\&` from rep against caps. */
23522365 size_t cap = caps[0] + rlen * 4 + (slen - caps[1]) + 64;
2353- char *out = sp_str_alloc_raw(cap);
2366+ /* malloc scratch: sp_re_expand_rep and the tail grow it with realloc,
2367+ which needs a real malloc base. Exact-sized string emitted below. */
2368+ char *out = (char *)malloc(cap);
23542369 memcpy(out, str, caps[0]);
23552370 size_t olen = caps[0];
23562371 sp_re_expand_rep(&out, &olen, &cap, rep, rlen, str, caps, n);
23572372 size_t rest = slen - caps[1];
23582373 if (olen + rest + 1 >= cap) { cap = olen + rest + 64; out = (char*)realloc(out, cap); }
2359- memcpy(out+olen, str+caps[1], rest);
2360- out[olen+rest] = 0;
2361- sp_str_set_len(out, olen + rest);
2362- return out;
2374+ memcpy(out+olen, str+caps[1], rest); olen += rest;
2375+ char *res = sp_str_alloc(olen);
2376+ memcpy(res, out, olen);
2377+ free(out);
2378+ return res;
23632379 }
23642380
23652381 static sp_StrArray *sp_re_scan(mrb_regexp_pattern *pat, const char *str) {