Fix memory leak for string object when out of memory

The `mrb_str_pool()` function has a path to call `malloc()` twice.

If occurs `NoMemoryError` exception in second `malloc()`,
first `malloc()` pointer is not freed.
This commit is contained in:
dearblue
2019-01-18 20:38:27 +09:00
parent fef1c152ce
commit 7a0418304e
+7 -1
View File
@@ -176,6 +176,8 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
mrb_free(mrb, irep);
}
mrb_noreturn void mrb_raise_nomemory(mrb_state *mrb);
mrb_value
mrb_str_pool(mrb_state *mrb, mrb_value str)
{
@@ -214,7 +216,11 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
ns->as.ary[len] = '\0';
}
else {
ns->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)len+1);
ns->as.heap.ptr = (char *)mrb_malloc_simple(mrb, (size_t)len+1);
if (!ns->as.heap.ptr) {
mrb_free(mrb, ns);
mrb_raise_nomemory(mrb);
}
ns->as.heap.len = len;
ns->as.heap.aux.capa = len;
if (ptr) {