summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-08 13:30:04 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-08 13:30:04 +0000
commit4e39dc864cc197923bf6e56b9359a1c4def7b1ae (patch)
tree23207320fc8686084ac145a122a217875fcc00cc /string.c
parent9a57bc8f55116d02c8294dcb4ebcddcfd56a006f (diff)
* 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
Diffstat (limited to 'string.c')
-rw-r--r--string.c16
1 files changed, 12 insertions, 4 deletions
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);
}