summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2020-06-23 13:19:10 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2020-06-25 06:57:43 +0900
commit9cdf439db52b66447b4e37c61179d54fad6c8f33 (patch)
treec89e9163d96b5ce6478bf054c02f19c0b407fc9d
parent95360a17f2260a46daeab2ed3456cd7c25c162ef (diff)
downloadmruby-9cdf439db52b66447b4e37c61179d54fad6c8f33.tar.gz
mruby-9cdf439db52b66447b4e37c61179d54fad6c8f33.zip
Free the original pointer if `realloc` failed.
The POSIX `realloc` keep the original pointer untouched, so it can easily leads to memory leakage. `mrb_realloc()` should handle those bookkeeping, while `mrb_realloc_simple()` keeps the original `realloc` behavior.
-rw-r--r--src/gc.c11
1 files changed, 3 insertions, 8 deletions
diff --git a/src/gc.c b/src/gc.c
index 03c561d35..6c83911d5 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -225,14 +225,9 @@ mrb_realloc(mrb_state *mrb, void *p, size_t len)
p2 = mrb_realloc_simple(mrb, p, len);
if (len == 0) return p2;
if (p2 == NULL) {
- if (mrb->gc.out_of_memory) {
- mrb_raise_nomemory(mrb);
- /* mrb_panic(mrb); */
- }
- else {
- mrb->gc.out_of_memory = TRUE;
- mrb_raise_nomemory(mrb);
- }
+ mrb_free(mrb, p);
+ mrb->gc.out_of_memory = TRUE;
+ mrb_raise_nomemory(mrb);
}
else {
mrb->gc.out_of_memory = FALSE;