From 4e39dc864cc197923bf6e56b9359a1c4def7b1ae Mon Sep 17 00:00:00 2001 From: nobu Date: Wed, 8 Feb 2012 13:30:04 +0000 Subject: * string.c (rb_str_modify_expand): fix memory leak. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@34492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- string.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'string.c') diff --git a/string.c b/string.c index 61b0a96ef6..d2737045e1 100644 --- a/string.c +++ b/string.c @@ -1328,12 +1328,20 @@ rb_str_modify_expand(VALUE str, long expand) if (expand < 0) { rb_raise(rb_eArgError, "negative expanding string size"); } - if (!str_independent(str) || - (expand > 0 && - (!STR_EMBED_P(str) || - RSTRING_LEN(str) + expand > RSTRING_EMBED_LEN_MAX))) { + if (!str_independent(str)) { str_make_independent_expand(str, expand); } + else if (expand > 0) { + long len = RSTRING_LEN(str); + long capa = len + expand; + if (!STR_EMBED_P(str)) { + REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+1); + RSTRING(str)->as.heap.aux.capa = capa; + } + else if (capa > RSTRING_EMBED_LEN_MAX) { + str_make_independent_expand(str, expand); + } + } ENC_CODERANGE_CLEAR(str); } -- cgit v1.2.3