Skip to content

Commit c9f23d2

Browse files
cphlipotacmel
authored andcommitted
perf event-parse: Use fixed size string for comms
Some implementations of libc do not support the 'm' width modifier as part of the scanf string format specifier. This can cause the parsing to fail. Since the parser never checks if the scanf parsing was successesful, this can result in a crash. Change the comm string to be allocated as a fixed size instead of dynamically using 'm' scanf width modifier. This can be safely done since comm size is limited to 16 bytes by TASK_COMM_LEN within the kernel. This change prevents perf from crashing when linked against bionic as well as reduces the total number of heap allocations and frees invoked while accomplishing the same task. Signed-off-by: Chris Phlipot <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent a72f642 commit c9f23d2

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

tools/perf/util/trace-event-parse.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,15 @@ void parse_ftrace_printk(struct tep_handle *pevent,
164164
void parse_saved_cmdline(struct tep_handle *pevent,
165165
char *file, unsigned int size __maybe_unused)
166166
{
167-
char *comm;
167+
char comm[17]; /* Max comm length in the kernel is 16. */
168168
char *line;
169169
char *next = NULL;
170170
int pid;
171171

172172
line = strtok_r(file, "\n", &next);
173173
while (line) {
174-
sscanf(line, "%d %ms", &pid, &comm);
175-
tep_register_comm(pevent, comm, pid);
176-
free(comm);
174+
if (sscanf(line, "%d %16s", &pid, comm) == 2)
175+
tep_register_comm(pevent, comm, pid);
177176
line = strtok_r(NULL, "\n", &next);
178177
}
179178
}

0 commit comments

Comments
 (0)