mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
+7
-1
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user