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
if (olen+before+rlen >= cap) { cap = (olen+before+rlen)*2+64; out = (char*)realloc(out, cap); } memcpy(out+olen, str+pos, before); olen += before; sp_re_expand_rep(&out, &olen, &cap, rep, rlen, str, caps, n); pos = caps[1]; if (caps[0] == caps[1]) pos++; if (caps[0] == caps[1]) { /* Zero-width match (/^/, /$/, /\b/, an empty pattern): Ruby inserts the replacement before the char at this position, keeps that char, and advances past it. Copy the char and step by one so the scan makes progress without dropping it or spinning on the same spot. */ if (caps[1] < slen) { if (olen+1 >= cap) { cap = olen*2+64; out = (char*)realloc(out, cap); } out[olen++] = str[caps[1]]; } pos = caps[1] + 1; } else { pos = caps[1]; } } size_t rest = slen - pos; if (olen+rest+1 >= cap) { cap = olen+rest+64; out = (char*)realloc(out, cap); } memcpy(out+olen, str+pos, rest); olen += rest; /* pos can land at slen+1 after a zero-width match at the end; guard the tail copy so `slen - pos` doesn't underflow size_t. */ if (pos < slen) { size_t rest = slen - pos; 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. */ memcpy(out + olen, str + pos, before); olen += before; memcpy(out + olen, rep, rlen); olen += rlen; if (key != keybuf) free(key); pos = caps[1]; if (caps[0] == caps[1]) pos++; if (caps[0] == caps[1]) { /* Zero-width match: keep the source char at this position and step past it (see sp_re_gsub for the rationale). */ if (caps[1] < slen) { if (olen + 1 >= cap) { cap = olen * 2 + 64; out = (char *)realloc(out, cap); } out[olen++] = str[caps[1]]; } pos = caps[1] + 1; } else { pos = caps[1]; } } if (pos < slen) { size_t rest = slen - pos; if (olen + rest >= cap) { cap = olen + rest + 1; out = (char *)realloc(out, cap); } memcpy(out + olen, str + pos, rest); olen += rest; } size_t rest = slen - pos; 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;}test/regexp_gsub_zero_width.rbaddedView file
Loading diff…
test/regexp_gsub_zero_width.rb.expectedaddedView file
Loading diff…