From: Ian Rogers <irogers@google.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>, Jiri Olsa <jolsa@kernel.org>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
Ian Rogers <irogers@google.com>
Subject: [PATCH v1 5/5] perf thread: Free the comm read from procfs
Date: Thu, 17 Sep 2026 23:32:49 -0700 [thread overview]
Message-ID: <20260918063249.2172589-6-irogers@google.com> (raw)
In-Reply-To: <20260918063249.2172589-1-irogers@google.com>
procfs__read_str() allocates the buffer it hands back and
thread__set_comm() only stores an interned copy of the string, so the
caller owns the buffer. thread__set_comm_from_proc() freed it when the
read came back empty but not once it had been used, leaking it on
every successful call.
Free it on both paths. The empty read still returns -1, err being
untouched in that case.
Found with leak sanitizer while running 'perf trace':
Direct leak of 7 byte(s) in 1 object(s) allocated from:
#1 io__getdelim fs/../io.h:179
#2 filename__read_str fs/fs.c:365
#3 procfs__read_str fs/fs.c:402
#4 thread__set_comm_from_proc util/thread.c:297
#5 syscall_arg__scnprintf_pid trace/beauty/pid.c:15
Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/thread.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index f0d3773d87db..fcf7c78ab767 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -296,12 +296,12 @@ int thread__set_comm_from_proc(struct thread *thread)
thread__pid(thread), thread__tid(thread)) >= (int)sizeof(path)) &&
procfs__read_str(path, &comm, &sz) == 0) {
/* sz==0: read got nothing, e.g. race during exit teardown */
- if (sz == 0) {
- free(comm);
- return -1;
+ if (sz > 0) {
+ comm[sz - 1] = '\0';
+ err = thread__set_comm(thread, comm, 0);
}
- comm[sz - 1] = '\0';
- err = thread__set_comm(thread, comm, 0);
+ /* thread__set_comm() copies the string, so release the buffer. */
+ free(comm);
}
return err;
--
2.55.0.1082.g2b9226bbc0-goog
prev parent reply other threads:[~2026-09-18 6:33 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 6:32 [PATCH v1 0/5] perf trace: Tracepoint format error handling and leak fixes Ian Rogers
2026-09-18 6:32 ` [PATCH v1 1/5] perf trace-event: Report tracepoint format errors with NULL and errno Ian Rogers
2026-09-18 6:32 ` [PATCH v1 2/5] perf trace-event: Reuse an already parsed tracepoint format Ian Rogers
2026-09-18 6:32 ` [PATCH v1 3/5] perf trace-event: Free the global trace_event when a command ends Ian Rogers
2026-09-18 6:32 ` [PATCH v1 4/5] perf trace: Free the host machine allocation Ian Rogers
2026-09-18 6:32 ` Ian Rogers [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260918063249.2172589-6-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®