Re: [PATCH] genetlink: Prevent memory leak when krealloc fail

Florian Westphal
2023-11-18T13:43:45Z

Kamil Duljas <kamil.duljas@gmail.com> wrote:
> Yes, you're right. I did not think about it. So if we have a static
> pointer that may be resued, should not restore the pointer as at the
> beginning?
> static unsigned long *mc_groups = &mc_group_start;
>
> At this moment we don't know how much memory is allocated. What do you
> think about this?

We do: mc_groups_longs.

> > new_groups = krealloc(mc_groups, nlen,
> > GFP_KERNEL);
> > - if (!new_groups)
> > + if (!new_groups) {
> > + kfree(mc_groups);
> > + mc_groups = &mc_group_start;
> > return -ENOMEM;
> > + }

Seems wrong to shrink when we can't grow. Whats the point?