From nobody Sat Jan 3 03:56:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7041E92FC9 for ; Thu, 5 Oct 2023 23:22:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229733AbjJEXWT (ORCPT ); Thu, 5 Oct 2023 19:22:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229692AbjJEXUa (ORCPT ); Thu, 5 Oct 2023 19:20:30 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52896D7B for ; Thu, 5 Oct 2023 16:09:38 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d8b2eec15d3so1972526276.1 for ; Thu, 05 Oct 2023 16:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696547377; x=1697152177; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=qYwGkCO2MUsLkIeTcNd4b7CxJky5rinvoIEFDCIq7TE=; b=AmygH3mhIGKPLin8SH2nsSFuBclbRF8DCk8KZ5z1+8inIU4t0OFZvWvAy0F/9pJaam +X71LhtnxiXOgVOBSDCq+5UeFL9b4yt4i7S5sfNWCkpdonHWsMFXHaBdJMjVvoBeGtoT +jflVR5R8dkmjcQKmYk81wOaBFQtqhz1P6xk9ilxZnouc5JP1g4JVzwBa8j8B9dOSeCh YqW/pHWV4RqaomBnOz7sSLgcdX27oLpj2l5XexX7cWLYrMaaKDgNAajaOa9CKqkAUjAb aoQIq/NmkzMmojW9sUOxZCFyrIgo4VyAZclr94JY8czCDgI+imWcRygyRRwXCi7Qzs0W T0/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696547377; x=1697152177; 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=qYwGkCO2MUsLkIeTcNd4b7CxJky5rinvoIEFDCIq7TE=; b=a3txneB5uto6oQmp9RwkjX2xoX6kfohTQO72Xl2i9O3Atyv9POHS7zKmLxFC1lRpxo 5NuVugdF4KJPiF5/S33+aItkAa3OelWdQn0rdOD18Hp97Vav/0Gasa/8QTtQlBO2wFNP 5hTNYzz0M0GUsJ/ymjF9lZLaMUndboPOxLSR740tPgECc0DQxLlCaFbBVlLvgEWTUqjI XS5JBshNeqxRVo3s1f6skyc8UPQ7fgfOf0RN4TBRhS485TAahDHm8rKc1YTxBNtL4qIW Hi01uWtxmDedEFHubPkEenPIxOQUPyF/QPzeiK9xqJGtRyV6pdSrPK6eFQlpnJ0oMMHz RqbA== X-Gm-Message-State: AOJu0YyE2uaeqKvqZjPFrV24tKMKdt2Yybay+ueF3DqMudaEeofxCG+k MXSwbtiag4I0B1vlU6zxRBE0TXwcyvdf X-Google-Smtp-Source: AGHT+IHWDvJDX0friGLPas8sSB1bU+Zqlv0548Stovx09NJffOSg70jhIDwPg6UsbunfjJZrgTn004zZHK20 X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:7449:56a1:2b14:305b]) (user=irogers job=sendgmr) by 2002:a25:8e8e:0:b0:d90:e642:d9fc with SMTP id q14-20020a258e8e000000b00d90e642d9fcmr96425ybl.6.1696547377572; Thu, 05 Oct 2023 16:09:37 -0700 (PDT) Date: Thu, 5 Oct 2023 16:08:50 -0700 In-Reply-To: <20231005230851.3666908-1-irogers@google.com> Message-Id: <20231005230851.3666908-18-irogers@google.com> 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 17/18] perf header: Fix various error path memory leaks 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Memory leaks were detected by clang-tidy. Signed-off-by: Ian Rogers --- tools/perf/util/header.c | 63 ++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c index d812e1e371a7..41b78e40b22b 100644 --- a/tools/perf/util/header.c +++ b/tools/perf/util/header.c @@ -2598,8 +2598,10 @@ static int process_cpu_topology(struct feat_fd *ff, = void *data __maybe_unused) goto error; =20 /* include a NULL character at the end */ - if (strbuf_add(&sb, str, strlen(str) + 1) < 0) + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) { + free(str); goto error; + } size +=3D string_size(str); free(str); } @@ -2617,8 +2619,10 @@ static int process_cpu_topology(struct feat_fd *ff, = void *data __maybe_unused) goto error; =20 /* include a NULL character at the end */ - if (strbuf_add(&sb, str, strlen(str) + 1) < 0) + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) { + free(str); goto error; + } size +=3D string_size(str); free(str); } @@ -2681,8 +2685,10 @@ static int process_cpu_topology(struct feat_fd *ff, = void *data __maybe_unused) goto error; =20 /* include a NULL character at the end */ - if (strbuf_add(&sb, str, strlen(str) + 1) < 0) + if (strbuf_add(&sb, str, strlen(str) + 1) < 0) { + free(str); goto error; + } size +=3D string_size(str); free(str); } @@ -2736,10 +2742,9 @@ static int process_numa_topology(struct feat_fd *ff,= void *data __maybe_unused) goto error; =20 n->map =3D perf_cpu_map__new(str); + free(str); if (!n->map) goto error; - - free(str); } ff->ph->env.nr_numa_nodes =3D nr; ff->ph->env.numa_nodes =3D nodes; @@ -2913,10 +2918,10 @@ static int process_cache(struct feat_fd *ff, void *= data __maybe_unused) return -1; =20 for (i =3D 0; i < cnt; i++) { - struct cpu_cache_level c; + struct cpu_cache_level *c =3D &caches[i]; =20 #define _R(v) \ - if (do_read_u32(ff, &c.v))\ + if (do_read_u32(ff, &c->v)) \ goto out_free_caches; \ =20 _R(level) @@ -2926,22 +2931,25 @@ static int process_cache(struct feat_fd *ff, void *= data __maybe_unused) #undef _R =20 #define _R(v) \ - c.v =3D do_read_string(ff); \ - if (!c.v) \ - goto out_free_caches; + c->v =3D do_read_string(ff); \ + if (!c->v) \ + goto out_free_caches; \ =20 _R(type) _R(size) _R(map) #undef _R - - caches[i] =3D c; } =20 ff->ph->env.caches =3D caches; ff->ph->env.caches_cnt =3D cnt; return 0; out_free_caches: + for (i =3D 0; i < cnt; i++) { + free(caches[i].type); + free(caches[i].size); + free(caches[i].map); + } free(caches); return -1; } @@ -3585,18 +3593,16 @@ static int perf_header__adds_write(struct perf_head= er *header, struct feat_copier *fc) { int nr_sections; - struct feat_fd ff; + struct feat_fd ff =3D { + .fd =3D fd, + .ph =3D header, + }; struct perf_file_section *feat_sec, *p; int sec_size; u64 sec_start; int feat; int err; =20 - ff =3D (struct feat_fd){ - .fd =3D fd, - .ph =3D header, - }; - nr_sections =3D bitmap_weight(header->adds_features, HEADER_FEAT_BITS); if (!nr_sections) return 0; @@ -3623,6 +3629,7 @@ static int perf_header__adds_write(struct perf_header= *header, err =3D do_write(&ff, feat_sec, sec_size); if (err < 0) pr_debug("failed to write feature section\n"); + free(ff.buf); free(feat_sec); return err; } @@ -3630,11 +3637,11 @@ static int perf_header__adds_write(struct perf_head= er *header, int perf_header__write_pipe(int fd) { struct perf_pipe_file_header f_header; - struct feat_fd ff; + struct feat_fd ff =3D { + .fd =3D fd, + }; int err; =20 - ff =3D (struct feat_fd){ .fd =3D fd }; - f_header =3D (struct perf_pipe_file_header){ .magic =3D PERF_MAGIC, .size =3D sizeof(f_header), @@ -3645,7 +3652,7 @@ int perf_header__write_pipe(int fd) pr_debug("failed to write perf pipe header\n"); return err; } - + free(ff.buf); return 0; } =20 @@ -3658,11 +3665,12 @@ static int perf_session__do_write_header(struct per= f_session *session, struct perf_file_attr f_attr; struct perf_header *header =3D &session->header; struct evsel *evsel; - struct feat_fd ff; + struct feat_fd ff =3D { + .fd =3D fd, + }; u64 attr_offset; int err; =20 - ff =3D (struct feat_fd){ .fd =3D fd}; lseek(fd, sizeof(f_header), SEEK_SET); =20 evlist__for_each_entry(session->evlist, evsel) { @@ -3670,6 +3678,7 @@ static int perf_session__do_write_header(struct perf_= session *session, err =3D do_write(&ff, evsel->core.id, evsel->core.ids * sizeof(u64)); if (err < 0) { pr_debug("failed to write perf header\n"); + free(ff.buf); return err; } } @@ -3695,6 +3704,7 @@ static int perf_session__do_write_header(struct perf_= session *session, err =3D do_write(&ff, &f_attr, sizeof(f_attr)); if (err < 0) { pr_debug("failed to write perf header attribute\n"); + free(ff.buf); return err; } } @@ -3705,8 +3715,10 @@ static int perf_session__do_write_header(struct perf= _session *session, =20 if (at_exit) { err =3D perf_header__adds_write(header, evlist, fd, fc); - if (err < 0) + if (err < 0) { + free(ff.buf); return err; + } } =20 f_header =3D (struct perf_file_header){ @@ -3728,6 +3740,7 @@ static int perf_session__do_write_header(struct perf_= session *session, =20 lseek(fd, 0, SEEK_SET); err =3D do_write(&ff, &f_header, sizeof(f_header)); + free(ff.buf); if (err < 0) { pr_debug("failed to write perf header\n"); return err; --=20 2.42.0.609.gbb76f46606-goog