1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

New buffer for shared string

* string.c (rb_str_init): allocate new buffer if the string is
  shared.  [Bug #15937]
This commit is contained in:
Nobuyoshi Nakada 2019-06-19 14:03:02 +09:00
parent 148f50fc78
commit 8797f48373
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 14 additions and 0 deletions

View file

@ -1616,6 +1616,15 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
memcpy(new_ptr, RSTRING(str)->as.ary, RSTRING_EMBED_LEN_MAX + 1);
RSTRING(str)->as.heap.ptr = new_ptr;
}
else if (FL_TEST(str, STR_SHARED|STR_NOFREE)) {
const size_t size = (size_t)capa + termlen;
const char *const old_ptr = RSTRING_PTR(str);
const size_t osize = RSTRING(str)->as.heap.len + TERM_LEN(str);
char *new_ptr = ALLOC_N(char, (size_t)capa + termlen);
memcpy(new_ptr, old_ptr, osize < size ? osize : size);
FL_UNSET_RAW(str, STR_SHARED);
RSTRING(str)->as.heap.ptr = new_ptr;
}
else if (STR_HEAP_SIZE(str) != (size_t)capa + termlen) {
SIZED_REALLOC_N(RSTRING(str)->as.heap.ptr, char,
(size_t)capa + termlen, STR_HEAP_SIZE(str));

View file

@ -79,6 +79,11 @@ class TestString < Test::Unit::TestCase
assert_equal("mystring", str.__send__(:initialize, str, capacity: 1000))
end
def test_initialize_shared
String.new(str = "mystring" * 10).__send__(:initialize, capacity: str.bytesize)
assert_equal("mystring", str[0, 8])
end
def test_initialize_nonstring
assert_raise(TypeError) {
S(1)