Skip to content

Commit

Permalink
Fix memory error on str_buf_cat
Browse files Browse the repository at this point in the history
Modify from nofree to embed string
  • Loading branch information
ksss committed Jan 2, 2017
1 parent 30b6648 commit 8a9a241
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,21 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
}
if (RSTR_NOFREE_P(s)) {
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) {
memcpy(RSTR_PTR(s), p, s->as.heap.len);
memcpy(RSTR_PTR(s), p, len);
}
RSTR_PTR(s)[s->as.heap.len] = '\0';
s->as.heap.aux.capa = s->as.heap.len;
RSTR_UNSET_NOFREE_FLAG(s);
RSTR_PTR(s)[len] = '\0';
return;
}
}
Expand Down

0 comments on commit 8a9a241

Please sign in to comment.