aboutsummaryrefslogtreecommitdiffstats
path: root/string.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-19 14:03:02 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-19 14:39:19 +0900
commit8797f48373dcfa3ff8e748667732dea8aea4347e (patch)
tree4a8a7e05b3e015b0c2e5e73328b05357d97c743c /string.c
parent148f50fc789d59319113a1fd2897dbe552e73e73 (diff)
downloadruby-8797f48373dcfa3ff8e748667732dea8aea4347e.tar.gz
New buffer for shared string
* string.c (rb_str_init): allocate new buffer if the string is shared. [Bug #15937]
Diffstat (limited to 'string.c')
-rw-r--r--string.c9
1 files changed, 9 insertions, 0 deletions
diff --git a/string.c b/string.c
index e402a5a7aa..3feeb8c705 100644
--- a/string.c
+++ b/string.c
@@ -1615,6 +1615,15 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
char *new_ptr = ALLOC_N(char, (size_t)capa + termlen);
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,