diff --git a/string.c b/string.c index e811e8aa9d..fdb7e4003c 100644 --- a/string.c +++ b/string.c @@ -2069,7 +2069,7 @@ static void str_make_independent_expand(VALUE str, long len, long expand, const int termlen) { char *ptr; - const char *oldptr; + char *oldptr; long capa = len + expand; if (len > capa) len = capa; @@ -2088,6 +2088,9 @@ str_make_independent_expand(VALUE str, long len, long expand, const int termlen) if (oldptr) { memcpy(ptr, oldptr, len); } + if (FL_TEST_RAW(str, STR_NOEMBED|STR_NOFREE|STR_SHARED) == STR_NOEMBED) { + xfree(oldptr); + } STR_SET_NOEMBED(str); FL_UNSET(str, STR_SHARED|STR_NOFREE); TERM_FILL(ptr + len, termlen);