Skip to content

Commit

Permalink
Fix memory leak reported in -test-/random/loop.c
Browse files Browse the repository at this point in the history
RUBY_TYPED_DEFAULT_FREE will only free the rand_loop_t, but it will cause
the buf to be leaked. This commit fixes the memory leak by implementing
a free function for the rand_loop_t type.
  • Loading branch information
peterzhu2118 committed Aug 12, 2024
1 parent 992596f commit 568d7ab
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ext/-test-/random/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ static const rb_random_interface_t random_loop_if = {
RB_RANDOM_INTERFACE_DEFINE_WITH_REAL(loop)
};

static void
loop_free(void *ptr)
{
rand_loop_t *r = ptr;

xfree(r->buf);
xfree(r);
}

RB_RANDOM_DEFINE_INIT_INT32_FUNC(loop)
static size_t
random_loop_memsize(const void *ptr)
Expand All @@ -25,7 +34,7 @@ static rb_random_data_type_t random_loop_type = {
"random/loop",
{
rb_random_mark,
RUBY_TYPED_DEFAULT_FREE,
loop_free,
random_loop_memsize,
},
RB_RANDOM_PARENT,
Expand Down

0 comments on commit 568d7ab

Please sign in to comment.