Actions
Bug #15937
closedSegmentation fault when String#initialize given same string with capacity field
Bug #15937:
Segmentation fault when String#initialize given same string with capacity field
Actions
Added by luke-gru (Luke Gruber) over 7 years ago. Updated about 7 years ago.
NOTE: I didn't attach a patch or proof of concept fix because I don't know how you want to handle this edge case. The easiest thing would just be to return the original string in rb_str_init and not honor the capacity change here.
I can't reproduce the buffer corruption nor the segfault, but found the content was cleared on a short string.
Which version did you try?
Applied in changeset git|28678997e40869f5591eae60edd9757334426ffb.
Preserve the string content at self-copying
Sorry, I forgot puts s in second script. I still get the segfault even in trunk with your patch, but the embedded small strings case is fixed.
Thank you.
I no longer get the segfault if in rb_str_init, return str is added right after existing check of (orig == str), but then capacity of string won't change.
EDIT: I think I figured out what was going on. If str is a shared string, it must be made independent before realloc in rb_str_init.
ruby_2_6 r67738 merged revision(s) 28678997e40869f5591eae60edd9757334426ffb,8797f48373dcfa3ff8e748667732dea8aea4347e.
ruby_2_5 r67769 merged revision(s) 28678997e40869f5591eae60edd9757334426ffb,8797f48373dcfa3ff8e748667732dea8aea4347e.