Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5051114001 for ; Thu, 5 Oct 2023 23:09:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="2EENq9ZG" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5a4f656f751so23141477b3.0 for ; Thu, 05 Oct 2023 16:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696547346; x=1697152146; darn=lists.linux.dev; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=aDPuyVd3XTrJMjMwlPDdlF9+59DlOSoA2gz1djDjGjM=; b=2EENq9ZGTf/RF4nq3LpMCscF1IjUQUrKQcxZUjprAEX43cKsVIWGRaHPbfk3+O/Znh ftVUe1y5ZpY6ALM6WqcmjJSI3wK9X6vwipmUzttw2QYuDweruoxwWNyqMbhXvn+NQX11 H86kaD74Ll5vWCLMZdhEDBtYKfHkjM37CHYbGY7Hrkr9W7L+hnYOrkGLUOsUMmWDG8ej SbXPXBMxYCP8iecMggLii1lgl5JcF63wgmBeetH5HIXP6Z2ZaTifnMISBZxpfEN1uNpQ BvCvydhOyBwxLygXBJIchFMBlMhuyNAALlrx687nz0bQQL6syjxp/vJPcLHX8FLtjQ6s G94w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696547346; x=1697152146; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=aDPuyVd3XTrJMjMwlPDdlF9+59DlOSoA2gz1djDjGjM=; b=CiBwx7DX/uRGgPibBqBMlv050oShdx5sMpctJxHdbh79vyxqCSevQIgl4KB7yxfzMR tsFQaUzFRSDXmKy0mPQ82OkpKTb0aOnSjhlOe2RYTfRKumXlmTY2aOasvGOLIttL38Le If3b68CH94dfmOdugjpp972n+R5NtpDKEBw5vSgklpEZhrOqEJdBNYBtpyPYsjJ5xj3v JzzLFNiibAgq8gVE8C0HDvVbijzT4DhK+/FoaBTCoi1phluO3dgFmMvS/PAW2aB8w8it UIxLDtdpQ4JpgmXSLvY+47sh+JvYJ2j6EDlWfApNRqHTzYanAMaaVSMhMUin4+RpDGhU enrQ== X-Gm-Message-State: AOJu0YwCYsgERP75YHKXFSZDxx0HQ4AgMmzsV/QIn29p9u4+pLBYMs+4 sw8+i7g8ZOTV2l9QCC5PU4qz/SSPHF/y X-Google-Smtp-Source: AGHT+IFQDjjuJ9i5zSFcUSzRWjUMmm/ajUwJIkmJtY7wD6WB6BJUTaAn4xhEtzWUY7ES/84RgxbXsLDpBnQp X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7449:56a1:2b14:305b]) (user=irogers job=sendgmr) by 2002:a05:690c:3146:b0:5a4:f657:3a95 with SMTP id fc6-20020a05690c314600b005a4f6573a95mr91738ywb.1.1696547346425; Thu, 05 Oct 2023 16:09:06 -0700 (PDT) Date: Thu, 5 Oct 2023 16:08:37 -0700 In-Reply-To: <20231005230851.3666908-1-irogers@google.com> Message-Id: <20231005230851.3666908-5-irogers@google.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20231005230851.3666908-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Subject: [PATCH v2 04/18] perf hisi-ptt: Fix potential memory leak From: Ian Rogers To: Nathan Chancellor , Nick Desaulniers , Tom Rix , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Yicong Yang , Jonathan Cameron , Yang Jihong , Kan Liang , Ming Wang , Huacai Chen , Sean Christopherson , K Prateek Nayak , Yanteng Si , Yuan Can , Ravi Bangoria , James Clark , llvm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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' ``` Signed-off-by: Ian Rogers --- 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