Commit c175ffea
c175ffea59c1Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Fix String#rpartition(regex) segfault
rpartition had no regex branch: a regex argument compiled to a NULL pointer that was passed to strlen/strstr, segfaulting. partition already had this branch; rpartition was missed.
Locate the last match by scanning forward with re_exec and keeping the rightmost match by start position (Ruby keeps the highest-start match, so "hello123world".rpartition(/\d+/) splits at "3", not "123"). The plain-separator path is preserved unchanged.
Fixes #1226
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- c175ffea59c1
- Parent
- 58f100045ab4
spinel_codegen.rbmodifiedView file
@@ -21196,13 +21196,36 @@
2119621196
register_tuple_type(tt)2119721197
@needs_gc = 12119821198
tname = tuple_c_name(tt)21199-
sep = compile_arg0(nid)2120021199
tmp = new_temp2120121200
emit(" " + tname + " *" + tmp + " = (" + tname + " *)sp_gc_alloc(sizeof(" + tname + "), NULL, " + tuple_scan_name(tt) + ");")21202-
emit(" { size_t _sl = strlen(" + rc + "), _pl = strlen(" + sep + "); const char *_last = NULL;")21203-
emit(" for (const char *_p = " + rc + "; (_p = strstr(_p, " + sep + ")); _p += _pl) _last = _p;")21204-
emit(" if (_last) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _last - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _last - " + rc + " + _pl, _sl - (_last - " + rc + ") - _pl); }")21205-
emit(" else { " + tmp + "->_0 = \"\"; " + tmp + "->_1 = \"\"; " + tmp + "->_2 = " + rc + "; } }")21201+
# Regex arg locates the last match via re_exec scanning forward and21202+
# keeping the rightmost; a plain separator path would pass the regex21203+
# pattern pointer to strstr/strlen and segfault (mirrors the21204+
# partition regex branch above).21205+
arg0_rpart = -121206+
if @nd_arguments[nid] >= 021207+
a_rpart = get_args(@nd_arguments[nid])21208+
if a_rpart.length > 021209+
arg0_rpart = a_rpart[0]21210+
end21211+
end21212+
rpat_rpart = arg0_rpart >= 0 ? regex_pat_c_expr(arg0_rpart) : ""21213+
if rpat_rpart != ""21214+
@needs_regexp = 121215+
emit(" { size_t _sl = strlen(" + rc + "); int _caps[2]; int _ms = -1, _me = -1; mrb_int _off = 0;")21216+
# rpartition keeps the rightmost match by start position. \d+ in21217+
# "hello123world" matches at 5/6/7, so advance by start+1 (not21218+
# end) to visit every start and retain the last (CRuby: "3").21219+
emit(" while (_off <= (mrb_int)_sl && re_exec(" + rpat_rpart + ", " + rc + ", _sl, _off, _caps, 2) > 0) { _ms = _caps[0]; _me = _caps[1]; _off = _caps[0] + 1; }")21220+
emit(" if (_ms >= 0) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _ms); " + tmp + "->_1 = sp_str_substr(" + rc + ", _ms, _me - _ms); " + tmp + "->_2 = sp_str_substr(" + rc + ", _me, _sl - _me); }")21221+
emit(" else { " + tmp + "->_0 = sp_str_empty; " + tmp + "->_1 = sp_str_empty; " + tmp + "->_2 = " + rc + "; } }")21222+
else21223+
sep = compile_arg0(nid)21224+
emit(" { size_t _sl = strlen(" + rc + "), _pl = strlen(" + sep + "); const char *_last = NULL;")21225+
emit(" for (const char *_p = " + rc + "; (_p = strstr(_p, " + sep + ")); _p += _pl) _last = _p;")21226+
emit(" if (_last) { " + tmp + "->_0 = sp_str_substr(" + rc + ", 0, _last - " + rc + "); " + tmp + "->_1 = " + sep + "; " + tmp + "->_2 = sp_str_substr(" + rc + ", _last - " + rc + " + _pl, _sl - (_last - " + rc + ") - _pl); }")21227+
emit(" else { " + tmp + "->_0 = sp_str_empty; " + tmp + "->_1 = sp_str_empty; " + tmp + "->_2 = " + rc + "; } }")21228+
end2120621229
return tmp2120721230
end2120821231
if mname == "hash"