Re: [PATCH v2 04/18] perf hisi-ptt: Fix potential memory leak

From: Namhyung Kim
Date: Mon Oct 09 2023 - 01:41:59 EST


On Thu, Oct 5, 2023 at 4:09 PM Ian Rogers <irogers@xxxxxxxxxx> 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@xxxxxxxxx

Thanks,
Namhyung


>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
> 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
>