summaryrefslogtreecommitdiff
path: root/parse.y
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
commit6e06d6440bc91dff08fdd7f07181468ebb1b39b3 (patch)
tree1e72e14f0ded7cff85d36ebfb14f13f50f525d5e /parse.y
parent8789eb02ee3a50bce083fbd3f1c1c8701014c979 (diff)
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 'parse.y')
-rw-r--r--parse.y20
1 files changed, 13 insertions, 7 deletions
diff --git a/parse.y b/parse.y
index fa9348d646..f134e15270 100644
--- a/parse.y
+++ b/parse.y
@@ -10968,24 +10968,30 @@ rb_parser_set_yydebug(VALUE self, VALUE flag)
#ifndef RIPPER
#ifdef YYMALLOC
#define HEAPCNT(n, size) ((n) * (size) / sizeof(YYSTYPE))
-#define ADD2HEAP(new, cnt, ptr) (p->heap = (new), (new)->cnt = (cnt), (ptr))
+/* Keep the order; NEWHEAP then xmalloc and ADD2HEAP to get rid of
+ * potential memory leak */
+#define NEWHEAP() rb_imemo_tmpbuf_parser_heap(0, p->heap, 0)
+#define ADD2HEAP(new, cnt, ptr) ((p->heap = (new))->ptr = (ptr), \
+ (new)->cnt = (cnt), (ptr))
void *
rb_parser_malloc(struct parser_params *p, size_t size)
{
size_t cnt = HEAPCNT(1, size);
+ rb_imemo_tmpbuf_t *n = NEWHEAP();
void *ptr = xmalloc(size);
- p->heap = rb_imemo_tmpbuf_parser_heap(ptr, p->heap, cnt);
- return p->heap->ptr;
+
+ return ADD2HEAP(n, cnt, ptr);
}
void *
rb_parser_calloc(struct parser_params *p, size_t nelem, size_t size)
{
size_t cnt = HEAPCNT(nelem, size);
+ rb_imemo_tmpbuf_t *n = NEWHEAP();
void *ptr = xcalloc(nelem, size);
- p->heap = rb_imemo_tmpbuf_parser_heap(ptr, p->heap, cnt);
- return p->heap->ptr;
+
+ return ADD2HEAP(n, cnt, ptr);
}
void *
@@ -11003,9 +11009,9 @@ rb_parser_realloc(struct parser_params *p, void *ptr, size_t size)
}
} while ((n = n->next) != NULL);
}
+ n = NEWHEAP();
ptr = xrealloc(ptr, size);
- p->heap = rb_imemo_tmpbuf_parser_heap(ptr, p->heap, cnt);
- return p->heap->ptr;
+ return ADD2HEAP(n, cnt, ptr);
}
void