Skip to content

Commit 3536c25

Browse files
captain5050acmel
authored andcommitted
perf thread: Fixes to thread__new() related to initializing comm
Freeing the thread on failure won't work with reference count checking, use thread__delete(). Don't allocate the comm_str, use a stack allocation instead. Fixes: f6005ca ("perf thread: Add reference count checking") Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Leo Yan <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 45b4f40 commit 3536c25

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

tools/perf/util/thread.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ int thread__init_maps(struct thread *thread, struct machine *machine)
3939

4040
struct thread *thread__new(pid_t pid, pid_t tid)
4141
{
42-
char *comm_str;
43-
struct comm *comm;
4442
RC_STRUCT(thread) *_thread = zalloc(sizeof(*_thread));
4543
struct thread *thread;
4644

4745
if (ADD_RC_CHK(thread, _thread) != NULL) {
46+
struct comm *comm;
47+
char comm_str[32];
48+
4849
thread__set_pid(thread, pid);
4950
thread__set_tid(thread, tid);
5051
thread__set_ppid(thread, -1);
@@ -56,13 +57,8 @@ struct thread *thread__new(pid_t pid, pid_t tid)
5657
init_rwsem(thread__namespaces_lock(thread));
5758
init_rwsem(thread__comm_lock(thread));
5859

59-
comm_str = malloc(32);
60-
if (!comm_str)
61-
goto err_thread;
62-
63-
snprintf(comm_str, 32, ":%d", tid);
60+
snprintf(comm_str, sizeof(comm_str), ":%d", tid);
6461
comm = comm__new(comm_str, 0, false);
65-
free(comm_str);
6662
if (!comm)
6763
goto err_thread;
6864

@@ -76,7 +72,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
7672
return thread;
7773

7874
err_thread:
79-
free(thread);
75+
thread__delete(thread);
8076
return NULL;
8177
}
8278

0 commit comments

Comments
 (0)