Re: [PATCH] genetlink: Prevent memory leak when krealloc fail
From: Florian Westphal
Date: Sat Nov 18 2023 - 08:44:00 EST
Kamil Duljas <kamil.duljas@xxxxxxxxx> 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?