summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-17 11:24:09 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-02-17 11:24:09 +0000
commit5a6a502ef9d88a77f701be8e0dde8fa489eb1e1f (patch)
tree89388b2f055743605308c11f382a87f40d8b2097 /string.c
parentd092fc539831062af3b87d2f4e29e94da3cc9b0e (diff)
string.c: fix rb_str_init
* string.c (rb_str_init): fix segfault and memory leak, consider wide char encoding terminator. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53855 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c48
1 files changed, 29 insertions, 19 deletions
diff --git a/string.c b/string.c
index 73e9492e7e..7850bcd1d5 100644
--- a/string.c
+++ b/string.c
@@ -163,6 +163,7 @@ static VALUE str_new_shared(VALUE klass, VALUE str);
static VALUE str_new_frozen(VALUE klass, VALUE orig);
static VALUE str_new_static(VALUE klass, const char *ptr, long len, int encindex);
static void str_make_independent_expand(VALUE str, long len, long expand, const int termlen);
+static inline void str_modifiable(VALUE str);
static inline void
str_make_independent(VALUE str)
@@ -1367,8 +1368,9 @@ static VALUE
rb_str_init(int argc, VALUE *argv, VALUE str)
{
static ID keyword_ids[2];
- VALUE orig, opt, enc, vcapa;
+ VALUE orig, opt, venc, vcapa;
VALUE kwargs[2];
+ rb_encoding *enc = 0;
int n;
if (!keyword_ids[0]) {
@@ -1379,45 +1381,53 @@ rb_str_init(int argc, VALUE *argv, VALUE str)
n = rb_scan_args(argc, argv, "01:", &orig, &opt);
if (!NIL_P(opt)) {
rb_get_kwargs(opt, keyword_ids, 0, 2, kwargs);
- enc = kwargs[0];
+ venc = kwargs[0];
vcapa = kwargs[1];
+ if (venc != Qundef && !NIL_P(venc)) {
+ enc = rb_to_encoding(venc);
+ }
if (vcapa != Qundef && !NIL_P(vcapa)) {
long capa = NUM2LONG(vcapa);
- str_discard(str);
+ long len = 0;
+ int termlen = enc ? rb_enc_mbminlen(enc) : 1;
+
if (capa < STR_BUF_MIN_SIZE) {
capa = STR_BUF_MIN_SIZE;
}
if (n == 1) {
- long len = RSTRING_LEN(orig);
+ StringValue(orig);
+ len = RSTRING_LEN(orig);
if (capa < len) {
capa = len;
}
- RSTRING(str)->as.heap.ptr = ALLOC_N(char, capa+1);
- memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), RSTRING_LEN(orig));
- RSTRING(str)->as.heap.len = len;
- rb_enc_cr_str_exact_copy(str, orig);
+ if (orig == str) n = 0;
}
- else {
- RSTRING(str)->as.heap.ptr = ALLOC_N(char, capa+1);
- RSTRING(str)->as.heap.ptr[0] = '\0';
+ str_modifiable(str);
+ if (STR_EMBED_P(str)) { /* make noembed always */
+ RSTRING(str)->as.heap.ptr = ALLOC_N(char, capa+termlen);
+ }
+ else if (STR_HEAP_SIZE(str) != capa+termlen) {
+ REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa+termlen);
+ }
+ RSTRING(str)->as.heap.len = len;
+ TERM_FILL(&RSTRING(str)->as.heap.ptr[len], termlen);
+ if (n == 1) {
+ memcpy(RSTRING(str)->as.heap.ptr, RSTRING_PTR(orig), len);
+ rb_enc_cr_str_exact_copy(str, orig);
}
FL_SET(str, STR_NOEMBED);
RSTRING(str)->as.heap.aux.capa = capa;
}
else if (n == 1) {
- StringValue(orig);
- str_discard(str);
- str_replace(str, orig);
+ rb_str_replace(str, orig);
}
- if (enc != Qundef && !NIL_P(enc)) {
- rb_enc_associate(str, rb_to_encoding(enc));
+ if (enc) {
+ rb_enc_associate(str, enc);
ENC_CODERANGE_CLEAR(str);
}
}
else if (n == 1) {
- StringValue(orig);
- str_discard(str);
- str_replace(str, orig);
+ rb_str_replace(str, orig);
}
return str;
}