Re: [PATCH 08/19] perf tools: Add mem2node object

From: Jiri Olsa
Date: Thu Mar 08 2018 - 08:18:15 EST


On Thu, Mar 08, 2018 at 09:58:49AM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

> > I don't think we need nentries.. AFAIK realloc works ok over single variable
>
> So:
>
> 1) you alloc entries with a max number of entries
>
> 2) you go on populating it
>
> 3) there are some left, lets shrink it:
>
> entries = realloc(entries, nr_entries * sizeof(entries[0]);
>
> Here it will probably not fail, but you check it anyway, and that is
> right, what happens if this returns NULL? entries gets set to NULL,
> we lose the reference to the allocated memory and you return -ENOMEM,
> right?
>
> We end up leaking entries when what I'm suggesting you to do is to
> not clobber entries with the return of realloc() (doing it this way most
> of the time leads to bugs), but instead store it to a temp var
> (nentries), and if it succeeds, then you know that you can
> set nentries to entries and go ahead with your nicely shrunk block of
> memory.
>
> If it fails, then you continue with the original block of memory, that
> continues to have what you just set up, etc.

ah that ;-) ok, will fix

thanks,
jirka