Commit ec7cd224

ec7cd2249fdcMerged

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

Description

Fix zero-width match handling in regex gsub

A zero-width regex match (/$/, /^/, /\b/, an empty pattern, a b*-style zero-repeat) made sp_re_gsub advance with a bare pos++, which dropped the source character at the match position and, after a zero-width match at the end of the string, left pos == slen + 1. The tail copy then computed rest = slen - pos, underflowing size_t into a huge length and memcpy'ing past the buffer -- heap corruption / crash. "x\ny".gsub(/$/, "!") aborted; "abc".gsub(/$/, "!") produced garbage.

On a zero-width match, copy the source character at the match position (Ruby inserts the replacement before it and keeps it) and advance by one, then guard the tail copy with pos < slen so the length can't underflow. Same fix applied to the gsub(regex, hash) variant.

Now "x\ny".gsub(/$/, "!") is "x!\ny!", "hello".gsub(//, "-") is "-h-e-l-l-o-", and gsub(/\b/, ...) works. Surfaced after #1308's ^/$ line-anchor fix made $ match before trailing newlines.

Refs #1308

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

Details

Commit
ec7cd2249fdc
Parent
89168c65acc3

lib/sp_runtime.hmodifiedView file

@@ -2227,11 +2227,27 @@
22272227 if (olen+before+rlen >= cap) { cap = (olen+before+rlen)*2+64; out = (char*)realloc(out, cap); }
22282228 memcpy(out+olen, str+pos, before); olen += before;
22292229 sp_re_expand_rep(&out, &olen, &cap, rep, rlen, str, caps, n);
2230- pos = caps[1]; if (caps[0] == caps[1]) pos++;
2230+ if (caps[0] == caps[1]) {
2231+ /* Zero-width match (/^/, /$/, /\b/, an empty pattern): Ruby inserts
2232+ the replacement before the char at this position, keeps that char,
2233+ and advances past it. Copy the char and step by one so the scan
2234+ makes progress without dropping it or spinning on the same spot. */
2235+ if (caps[1] < slen) {
2236+ if (olen+1 >= cap) { cap = olen*2+64; out = (char*)realloc(out, cap); }
2237+ out[olen++] = str[caps[1]];
2238+ }
2239+ pos = caps[1] + 1;
2240+ } else {
2241+ pos = caps[1];
2242+ }
22312243 }
2232- size_t rest = slen - pos;
2233- if (olen+rest+1 >= cap) { cap = olen+rest+64; out = (char*)realloc(out, cap); }
2234- memcpy(out+olen, str+pos, rest); olen += rest;
2244+ /* pos can land at slen+1 after a zero-width match at the end; guard the
2245+ tail copy so `slen - pos` doesn't underflow size_t. */
2246+ if (pos < slen) {
2247+ size_t rest = slen - pos;
2248+ if (olen+rest+1 >= cap) { cap = olen+rest+64; out = (char*)realloc(out, cap); }
2249+ memcpy(out+olen, str+pos, rest); olen += rest;
2250+ }
22352251 /* The buffer is over-allocated (cap has slack); set the string's
22362252 stored length to the bytes actually written so #length / inspect
22372253 don't read the trailing slack. */
@@ -2262,11 +2278,23 @@
22622278 memcpy(out + olen, str + pos, before); olen += before;
22632279 memcpy(out + olen, rep, rlen); olen += rlen;
22642280 if (key != keybuf) free(key);
2265- pos = caps[1]; if (caps[0] == caps[1]) pos++;
2281+ if (caps[0] == caps[1]) {
2282+ /* Zero-width match: keep the source char at this position and step
2283+ past it (see sp_re_gsub for the rationale). */
2284+ if (caps[1] < slen) {
2285+ if (olen + 1 >= cap) { cap = olen * 2 + 64; out = (char *)realloc(out, cap); }
2286+ out[olen++] = str[caps[1]];
2287+ }
2288+ pos = caps[1] + 1;
2289+ } else {
2290+ pos = caps[1];
2291+ }
2292+ }
2293+ if (pos < slen) {
2294+ size_t rest = slen - pos;
2295+ if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); }
2296+ memcpy(out + olen, str + pos, rest); olen += rest;
22662297 }
2267- size_t rest = slen - pos;
2268- if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); }
2269- memcpy(out + olen, str + pos, rest); olen += rest;
22702298 out[olen] = 0; sp_str_set_len(out, olen); return out;
22712299 }
22722300