Should allocate memory region before updating len; fix #3842

Otherwise half-baked string object will be allocated.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2017-11-08 08:46:08 +09:00
parent 7ae20e0785
commit f723832ebc
+1 -1
View File
@@ -70,9 +70,9 @@ str_new(mrb_state *mrb, const char *p, size_t len)
if (len >= MRB_INT_MAX) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "string size too big");
}
s->as.heap.ptr = (char *)mrb_malloc(mrb, len+1);
s->as.heap.len = (mrb_int)len;
s->as.heap.aux.capa = (mrb_int)len;
s->as.heap.ptr = (char *)mrb_malloc(mrb, len+1);
if (p) {
memcpy(s->as.heap.ptr, p, len);
}