summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-16 22:42:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2010-08-16 22:42:46 +0000
commit94fc95c8b04df32db8d473be058d4a98b72ab8ec (patch)
tree30c20fbe1b55153fd54d39f1e97f0d29f386b33b /string.c
parent0afddf7b0a8ebb1695b87a4aee75c06544bd5563 (diff)
Tue Aug 17 07:42:43 2010 Nobuyoshi Nakada <nobu@ruby-lang.org>
* string.c (str_make_independent_expand): set capacity properly. a patch from Peter Weldon at [ruby-core:31734]. [ruby-core:31653] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29018 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c11
1 files changed, 6 insertions, 5 deletions
diff --git a/string.c b/string.c
index 44f3131e7a..d18f47ee46 100644
--- a/string.c
+++ b/string.c
@@ -1268,18 +1268,19 @@ str_make_independent_expand(VALUE str, long expand)
{
char *ptr;
long len = RSTRING_LEN(str);
+ long capa = len + expand;
- ptr = ALLOC_N(char, len+expand+1);
+ if (len > capa) len = capa;
+ ptr = ALLOC_N(char, capa + 1);
if (RSTRING_PTR(str)) {
- memcpy(ptr, RSTRING_PTR(str), expand < 0 ? len + expand : len);
+ memcpy(ptr, RSTRING_PTR(str), len);
}
- len += expand;
STR_SET_NOEMBED(str);
+ STR_UNSET_NOCAPA(str);
ptr[len] = 0;
RSTRING(str)->as.heap.ptr = ptr;
RSTRING(str)->as.heap.len = len;
- RSTRING(str)->as.heap.aux.capa = len;
- STR_UNSET_NOCAPA(str);
+ RSTRING(str)->as.heap.aux.capa = capa;
}
#define str_make_independent(str) str_make_independent_expand(str, 0L)