Skip to content

Commit ec72769

Browse files
acmelgregkh
authored andcommitted
perf annotate: Use asprintf when formatting objdump command line
commit 6810158 upstream. We were using a local buffer with an arbitrary size, that would have to get increased to avoid truncation as warned by gcc 8: util/annotate.c: In function 'symbol__disassemble': util/annotate.c:1488:4: error: '%s' directive output may be truncated writing up to 4095 bytes into a region of size between 3966 and 8086 [-Werror=format-truncation=] "%s %s%s --start-address=0x%016" PRIx64 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ util/annotate.c:1498:20: symfs_filename, symfs_filename); ~~~~~~~~~~~~~~ util/annotate.c:1490:50: note: format string is defined here " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand", ^~ In file included from /usr/include/stdio.h:861, from util/color.h:5, from util/sort.h:8, from util/annotate.c:14: /usr/include/bits/stdio2.h:67:10: note: '__builtin___snprintf_chk' output 116 or more bytes (assuming 8331) into a destination of size 8192 return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ __bos (__s), __fmt, __va_arg_pack ()); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ So switch to asprintf, that will make sure enough space is available. Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jin Yao <yao.jin@linux.intel.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Link: https://lkml.kernel.org/n/tip-qagoy2dmbjpc9gdnaj0r3mml@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Ignat Korchagin <ignat@cloudflare.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 79f87e0 commit ec72769

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

tools/perf/util/annotate.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
14321432
struct arch **parch, char *cpuid)
14331433
{
14341434
struct dso *dso = map->dso;
1435-
char command[PATH_MAX * 2];
1435+
char *command;
14361436
struct arch *arch = NULL;
14371437
FILE *file;
14381438
char symfs_filename[PATH_MAX];
@@ -1496,7 +1496,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
14961496
strcpy(symfs_filename, tmp);
14971497
}
14981498

1499-
snprintf(command, sizeof(command),
1499+
err = asprintf(&command,
15001500
"%s %s%s --start-address=0x%016" PRIx64
15011501
" --stop-address=0x%016" PRIx64
15021502
" -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand",
@@ -1509,12 +1509,17 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
15091509
symbol_conf.annotate_src ? "-S" : "",
15101510
symfs_filename, symfs_filename);
15111511

1512+
if (err < 0) {
1513+
pr_err("Failure allocating memory for the command to run\n");
1514+
goto out_remove_tmp;
1515+
}
1516+
15121517
pr_debug("Executing: %s\n", command);
15131518

15141519
err = -1;
15151520
if (pipe(stdout_fd) < 0) {
15161521
pr_err("Failure creating the pipe to run %s\n", command);
1517-
goto out_remove_tmp;
1522+
goto out_free_command;
15181523
}
15191524

15201525
pid = fork();
@@ -1541,7 +1546,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
15411546
* If we were using debug info should retry with
15421547
* original binary.
15431548
*/
1544-
goto out_remove_tmp;
1549+
goto out_free_command;
15451550
}
15461551

15471552
nline = 0;
@@ -1570,6 +1575,8 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
15701575

15711576
fclose(file);
15721577
err = 0;
1578+
out_free_command:
1579+
free(command);
15731580
out_remove_tmp:
15741581
close(stdout_fd[0]);
15751582

@@ -1583,7 +1590,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map,
15831590

15841591
out_close_stdout:
15851592
close(stdout_fd[1]);
1586-
goto out_remove_tmp;
1593+
goto out_free_command;
15871594
}
15881595

15891596
static void insert_source_line(struct rb_root *root, struct source_line *src_line)

0 commit comments

Comments
 (0)