Re: [PATCH] perf hisi-ptt: Fix memory leak in lseek failure handling

From: Jonathan Cameron
Date: Fri Oct 06 2023 - 04:10:09 EST


On Mon, 2 Oct 2023 21:48:14 -0700
Namhyung Kim <namhyung@xxxxxxxxxx> wrote:

> Hello,
>
> On Sat, Sep 30, 2023 at 12:27 AM Kuan-Wei Chiu <visitorckw@xxxxxxxxx> wrote:
> >
> > In the previous code, there was a memory leak issue where the previously
> > allocated memory was not freed upon a failed lseek operation. This patch
> > addresses the problem by releasing the old memory before returning -errno
> > in case of a lseek failure. This ensures that memory is properly managed
> > and avoids potential memory leaks.
> >
> > Signed-off-by: Kuan-Wei Chiu <visitorckw@xxxxxxxxx>
>
> Acked-by: Namhyung Kim <namhyung@xxxxxxxxxx>
>
FYI, Ian Rogers has a series with a similar fix that goes a little further
and drops the assignment of data_offset seen just at the top of the context
below as it is unused.

https://lore.kernel.org/all/20231005230851.3666908-5-irogers@xxxxxxxxxx/

I haven't replied to this yet as want to check some related handling
with one of my China based colleagues and it's a festival this week.

One of us will get back on this next week,

Jonathan

> Thanks,
> Namhyung
>
>
> > ---
> > tools/perf/util/hisi-ptt.c | 4 +++-
> > 1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/perf/util/hisi-ptt.c b/tools/perf/util/hisi-ptt.c
> > index 45b614bb73bf..43bd1ca62d58 100644
> > --- a/tools/perf/util/hisi-ptt.c
> > +++ b/tools/perf/util/hisi-ptt.c
> > @@ -108,8 +108,10 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
> > data_offset = 0;
> > } else {
> > data_offset = lseek(fd, 0, SEEK_CUR);
> > - if (data_offset == -1)
> > + if (data_offset == -1) {
> > + free(data);
> > return -errno;
> > + }
> > }
> >
> > err = readn(fd, data, size);
> > --
> > 2.25.1
> >
>