aboutsummaryrefslogtreecommitdiffstats
path: root/gc.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-05-09 17:40:04 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-05-09 17:40:04 +0000
commit5229fc53173898172c117000805a1df1ede1ccdf (patch)
tree1e72e14f0ded7cff85d36ebfb14f13f50f525d5e /gc.c
parent8c50694ad4cdb1b1cb99fe47c3bf4211045aee76 (diff)
downloadruby-5229fc53173898172c117000805a1df1ede1ccdf.tar.gz
fix potential memory leaks
* gc.c (rb_alloc_tmp_buffer_with_count): keep the order; allocate an empty imemo first then xmalloc, to get rid of potential memory leak when allocation imemo failed. * parse.y (rb_parser_malloc, rb_parser_calloc, rb_parser_realloc): ditto. * process.c (rb_execarg_allocate_dup2_tmpbuf): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63385 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/gc.c b/gc.c
index 1d7ad561f4..bd52966bf1 100644
--- a/gc.c
+++ b/gc.c
@@ -8141,9 +8141,17 @@ void *
rb_alloc_tmp_buffer_with_count(volatile VALUE *store, size_t size, size_t cnt)
{
void *ptr;
+ VALUE imemo;
+ rb_imemo_tmpbuf_t *tmpbuf;
+ /* Keep the order; allocate an empty imemo first then xmalloc, to
+ * get rid of potential memory leak */
+ imemo = rb_imemo_tmpbuf_auto_free_maybe_mark_buffer(NULL, 0);
+ *store = imemo;
ptr = ruby_xmalloc0(size);
- *store = rb_imemo_tmpbuf_auto_free_maybe_mark_buffer(ptr, cnt);
+ tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
+ tmpbuf->ptr = ptr;
+ tmpbuf->cnt = cnt;
return ptr;
}