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
size_t rlen = strlen(r); size_t bl = sp_str_byte_len(s); size_t cap = bl + 64; char *out = sp_str_alloc_raw(cap); /* malloc scratch (grown with realloc on invalid-byte runs); the final string is emitted at the exact length below. */ char *out = (char *)malloc(cap); size_t olen = 0; size_t i = 0; while (i < bl) { i += 1; } } out[olen] = 0; sp_str_set_len(out, olen); return out; char *res = sp_str_alloc(olen); memcpy(res, out, olen); free(out); return res;}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;}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;}static const char *sp_re_gsub(mrb_regexp_pattern *pat, const char *str, const char *rep) { int64_t slen = (int64_t)strlen(str); size_t rlen = strlen(rep); size_t cap = slen * 2 + rlen * 4 + 64; char *out = sp_str_alloc_raw(cap); size_t olen = 0; /* Build into a plain malloc scratch: the buffer is grown with realloc here and inside sp_re_expand_rep, which is only valid on a real malloc base (a sp_str body pointer is offset past its header). The final string is allocated at the exact length below. */ char *out = (char *)malloc(cap); size_t olen = 0; int64_t pos = 0; int caps[64]; while (pos <= slen) { int n = re_exec(pat, str, slen, pos, caps, 64); if (olen+rest+1 >= cap) { cap = olen+rest+64; out = (char*)realloc(out, cap); } memcpy(out+olen, str+pos, rest); olen += rest; } /* The buffer is over-allocated (cap has slack); set the string's stored length to the bytes actually written so #length / inspect don't read the trailing slack. */ out[olen] = 0; sp_str_set_len(out, olen); return out; /* Emit a string sized to exactly the bytes written (sp_str_alloc sets the length and null-terminates); release the scratch. */ char *res = sp_str_alloc(olen); memcpy(res, out, olen); free(out); return res;}/* String#gsub(regex, hash) — per-match hash lookup form. CRuby's * Used by html_escape / json_escape idioms (gsub(/[&<>]/, ESCAPES)). */static const char *sp_re_gsub_str_str_hash(mrb_regexp_pattern *pat, const char *str, sp_StrStrHash *h) { int64_t slen = (int64_t)strlen(str); size_t cap = slen * 2 + 64; char *out = sp_str_alloc_raw(cap); size_t olen = 0; /* malloc scratch (realloc-safe); exact-sized string emitted below. */ size_t cap = slen * 2 + 64; char *out = (char *)malloc(cap); size_t olen = 0; int64_t pos = 0; int caps[64]; while (pos <= slen) { int n = re_exec(pat, str, slen, pos, caps, 64); if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); } memcpy(out + olen, str + pos, rest); olen += rest; } out[olen] = 0; sp_str_set_len(out, olen); return out; char *res = sp_str_alloc(olen); memcpy(res, out, olen); free(out); return res;}/* Issue #910: sub(regex, hash) — same lookup semantics as if (n <= 0 || caps[0] < 0) return str; /* Issue #855: expand `\1`..`\9` / `\&` from rep against caps. */ size_t cap = caps[0] + rlen * 4 + (slen - caps[1]) + 64; char *out = sp_str_alloc_raw(cap); /* malloc scratch: sp_re_expand_rep and the tail grow it with realloc, which needs a real malloc base. Exact-sized string emitted below. */ char *out = (char *)malloc(cap); memcpy(out, str, caps[0]); size_t olen = caps[0]; sp_re_expand_rep(&out, &olen, &cap, rep, rlen, str, caps, n); size_t rest = slen - caps[1]; if (olen + rest + 1 >= cap) { cap = olen + rest + 64; out = (char*)realloc(out, cap); } memcpy(out+olen, str+caps[1], rest); out[olen+rest] = 0; sp_str_set_len(out, olen + rest); return out; memcpy(out+olen, str+caps[1], rest); olen += rest; char *res = sp_str_alloc(olen); memcpy(res, out, olen); free(out); return res;}static sp_StrArray *sp_re_scan(mrb_regexp_pattern *pat, const char *str) {test/regexp_sub_gsub_realloc.rbaddedView file
Loading diff…
test/regexp_sub_gsub_realloc.rb.expectedaddedView file
Loading diff…