perf annotate: Use asprintf when formatting objdump command line
authorArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 30 Jun 2020 14:45:49 +0000 (23:45 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 31 Jul 2020 14:44:07 +0000 (16:44 +0200)
commit 6810158d526e483868e519befff407b91e76b3db 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>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
tools/perf/util/annotate.c

index 3336cbc6ec48f3c7cf68f9dd3087c3df769f1895..1d4807c46efdf08e3bd6f8f3404fb24cdea36838 100644 (file)
@@ -1302,7 +1302,7 @@ fallback:
 int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
 {
        struct dso *dso = map->dso;
-       char command[PATH_MAX * 2];
+       char *command;
        FILE *file;
        char symfs_filename[PATH_MAX];
        struct kcore_extract kce;
@@ -1364,7 +1364,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
                strcpy(symfs_filename, tmp);
        }
 
-       snprintf(command, sizeof(command),
+       err = asprintf(&command,
                 "%s %s%s --start-address=0x%016" PRIx64
                 " --stop-address=0x%016" PRIx64
                 " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
@@ -1377,12 +1377,17 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
                 symbol_conf.annotate_src ? "-S" : "",
                 symfs_filename, symfs_filename);
 
+       if (err < 0) {
+               pr_err("Failure allocating memory for the command to run\n");
+               goto out_remove_tmp;
+       }
+
        pr_debug("Executing: %s\n", command);
 
        err = -1;
        if (pipe(stdout_fd) < 0) {
                pr_err("Failure creating the pipe to run %s\n", command);
-               goto out_remove_tmp;
+               goto out_free_command;
        }
 
        pid = fork();
@@ -1409,7 +1414,7 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
                 * If we were using debug info should retry with
                 * original binary.
                 */
-               goto out_remove_tmp;
+               goto out_free_command;
        }
 
        nline = 0;
@@ -1432,6 +1437,8 @@ int symbol__disassemble(struct symbol *sym, struct map *map, size_t privsize)
 
        fclose(file);
        err = 0;
+out_free_command:
+       free(command);
 out_remove_tmp:
        close(stdout_fd[0]);
 
@@ -1445,7 +1452,7 @@ out:
 
 out_close_stdout:
        close(stdout_fd[1]);
-       goto out_remove_tmp;
+       goto out_free_command;
 }
 
 static void insert_source_line(struct rb_root *root, struct source_line *src_line)