Commit 158a6ab2 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3367 from ksss/str-modify

Fix segv on str_buf_cat
parents 07167b8f 8a9a2415
...@@ -154,10 +154,7 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len) ...@@ -154,10 +154,7 @@ str_buf_cat(mrb_state *mrb, struct RString *s, const char *ptr, size_t len)
off = ptr - RSTR_PTR(s); off = ptr - RSTR_PTR(s);
} }
if (RSTR_EMBED_P(s)) capa = RSTR_CAPA(s);
capa = RSTRING_EMBED_LEN_MAX;
else
capa = s->as.heap.aux.capa;
if (capa <= RSTRING_EMBED_LEN_MAX) if (capa <= RSTRING_EMBED_LEN_MAX)
capa = RSTRING_EMBED_LEN_MAX+1; capa = RSTRING_EMBED_LEN_MAX+1;
...@@ -698,14 +695,21 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) ...@@ -698,14 +695,21 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
} }
if (RSTR_NOFREE_P(s)) { if (RSTR_NOFREE_P(s)) {
char *p = s->as.heap.ptr; char *p = s->as.heap.ptr;
mrb_int len = s->as.heap.len;
s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)s->as.heap.len+1); RSTR_UNSET_NOFREE_FLAG(s);
if (len < RSTRING_EMBED_LEN_MAX) {
RSTR_SET_EMBED_FLAG(s);
RSTR_SET_EMBED_LEN(s, len);
}
else {
s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)len+1);
s->as.heap.aux.capa = len;
}
if (p) { if (p) {
memcpy(RSTR_PTR(s), p, s->as.heap.len); memcpy(RSTR_PTR(s), p, len);
} }
RSTR_PTR(s)[s->as.heap.len] = '\0'; RSTR_PTR(s)[len] = '\0';
s->as.heap.aux.capa = s->as.heap.len;
RSTR_UNSET_NOFREE_FLAG(s);
return; return;
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment