[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAM9d7ch4SLLbORdhkanCoPQZX=f-p-HxsYX2YWYbtLR4beD4wg@mail.gmail.com>
Date: Sun, 8 Oct 2023 22:41:40 -0700
From: Namhyung Kim <namhyung@...nel.org>
To: Ian Rogers <irogers@...gle.com>
Cc: Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Tom Rix <trix@...hat.com>,
Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Adrian Hunter <adrian.hunter@...el.com>,
Yicong Yang <yangyicong@...ilicon.com>,
Jonathan Cameron <jonathan.cameron@...wei.com>,
Yang Jihong <yangjihong1@...wei.com>,
Kan Liang <kan.liang@...ux.intel.com>,
Ming Wang <wangming01@...ngson.cn>,
Huacai Chen <chenhuacai@...nel.org>,
Sean Christopherson <seanjc@...gle.com>,
K Prateek Nayak <kprateek.nayak@....com>,
Yanteng Si <siyanteng@...ngson.cn>,
Yuan Can <yuancan@...wei.com>,
Ravi Bangoria <ravi.bangoria@....com>,
James Clark <james.clark@....com>, llvm@...ts.linux.dev,
linux-kernel@...r.kernel.org, linux-perf-users@...r.kernel.org,
bpf@...r.kernel.org
Subject: Re: [PATCH v2 04/18] perf hisi-ptt: Fix potential memory leak
On Thu, Oct 5, 2023 at 4:09 PM Ian Rogers <irogers@...gle.com> wrote:
>
> Fix clang-tidy found potential memory leak and unread value:
> ```
> tools/perf/util/hisi-ptt.c:108:3: warning: Value stored to 'data_offset' is never read [clang-analyzer-deadcode.DeadStores]
> data_offset = 0;
> ^ ~
> tools/perf/util/hisi-ptt.c:108:3: note: Value stored to 'data_offset' is never read
> data_offset = 0;
> ^ ~
> tools/perf/util/hisi-ptt.c:112:12: warning: Potential leak of memory pointed to by 'data' [clang-analyzer-unix.Malloc]
> return -errno;
> ^
> /usr/include/errno.h:38:18: note: expanded from macro 'errno'
> ^
> tools/perf/util/hisi-ptt.c:100:15: note: Memory is allocated
> void *data = malloc(size);
> ^~~~~~~~~~~~
> tools/perf/util/hisi-ptt.c:104:6: note: Assuming 'data' is non-null
> if (!data)
> ^~~~~
> tools/perf/util/hisi-ptt.c:104:2: note: Taking false branch
> if (!data)
> ^
> tools/perf/util/hisi-ptt.c:107:6: note: Assuming the condition is false
> if (perf_data__is_pipe(session->data)) {
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> tools/perf/util/hisi-ptt.c:107:2: note: Taking false branch
> if (perf_data__is_pipe(session->data)) {
> ^
> tools/perf/util/hisi-ptt.c:111:7: note: Assuming the condition is true
> if (data_offset == -1)
> ^~~~~~~~~~~~~~~~~
> tools/perf/util/hisi-ptt.c:111:3: note: Taking true branch
> if (data_offset == -1)
> ^
> tools/perf/util/hisi-ptt.c:112:12: note: Potential leak of memory pointed to by 'data'
> return -errno;
> ^
> /usr/include/errno.h:38:18: note: expanded from macro 'errno'
> ```
We already have
https://lore.kernel.org/r/20230930072719.1267784-1-visitorckw@gmail.com
Thanks,
Namhyung
>
> Signed-off-by: Ian Rogers <irogers@...gle.com>
> ---
> tools/perf/util/hisi-ptt.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/tools/perf/util/hisi-ptt.c b/tools/perf/util/hisi-ptt.c
> index 45b614bb73bf..ea297329c526 100644
> --- a/tools/perf/util/hisi-ptt.c
> +++ b/tools/perf/util/hisi-ptt.c
> @@ -98,18 +98,18 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
> int fd = perf_data__fd(session->data);
> int size = event->auxtrace.size;
> void *data = malloc(size);
> - off_t data_offset;
> int err;
>
> if (!data)
> return -errno;
>
> - if (perf_data__is_pipe(session->data)) {
> - data_offset = 0;
> - } else {
> - data_offset = lseek(fd, 0, SEEK_CUR);
> - if (data_offset == -1)
> + if (!perf_data__is_pipe(session->data)) {
> + off_t data_offset = lseek(fd, 0, SEEK_CUR);
> +
> + if (data_offset == -1) {
> + free(data);
> return -errno;
> + }
> }
>
> err = readn(fd, data, size);
> --
> 2.42.0.609.gbb76f46606-goog
>
Powered by blists - more mailing lists