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
commit83c7d9df2b0fce04c9f06911d0ed21153ba29fa2 (patch)
tree0d55208ab75dbd64cda593ec5700e9183a0d2531 /string.c
parent54b90b7be8d38fea97251a89f23380facb8d7475 (diff)
* string.c (rb_str_modify_expand): fix memory leak.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@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 9fd2480fd3..2ce6504fe1 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);
}