perf tools: Fix snprint warnings for gcc 8
authorJiri Olsa <jolsa@kernel.org>
Mon, 19 Mar 2018 08:29:01 +0000 (09:29 +0100)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 19 Mar 2018 13:00:43 +0000 (10:00 -0300)
With gcc 8 we get new set of snprintf() warnings that breaks the
compilation, one example:

  tests/mem.c: In function ‘check’:
  tests/mem.c:19:48: error: ‘%s’ directive output may be truncated writing \
        up to 99 bytes into a region of size 89 [-Werror=format-truncation=]
    snprintf(failure, sizeof failure, "unexpected %s", out);

The gcc docs says:

 To avoid the warning either use a bigger buffer or handle the
 function's return value which indicates whether or not its output
 has been truncated.

Given that all these warnings are harmless, because the code either
properly fails due to uncomplete file path or we don't care for
truncated output at all, I'm changing all those snprintf() calls to
scnprintf(), which actually 'checks' for the snprint return value so the
gcc stays silent.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Link: http://lkml.kernel.org/r/20180319082902.4518-1-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-script.c
tools/perf/tests/attr.c
tools/perf/tests/mem.c
tools/perf/tests/pmu.c
tools/perf/util/cgroup.c
tools/perf/util/parse-events.c
tools/perf/util/pmu.c

index cce926aeb0c08e16a6da828e4eff5f88b9fb36e5..313c424233938cc118f59e6e55950a5e0de22faa 100644 (file)
@@ -2674,8 +2674,8 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
        }
 
        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
-               snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
-                        lang_dirent->d_name);
+               scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
+                         lang_dirent->d_name);
                lang_dir = opendir(lang_path);
                if (!lang_dir)
                        continue;
@@ -2684,8 +2684,8 @@ static int list_available_scripts(const struct option *opt __maybe_unused,
                        script_root = get_script_root(script_dirent, REPORT_SUFFIX);
                        if (script_root) {
                                desc = script_desc__findnew(script_root);
-                               snprintf(script_path, MAXPATHLEN, "%s/%s",
-                                        lang_path, script_dirent->d_name);
+                               scnprintf(script_path, MAXPATHLEN, "%s/%s",
+                                         lang_path, script_dirent->d_name);
                                read_script_info(desc, script_path);
                                free(script_root);
                        }
@@ -2721,7 +2721,7 @@ static int check_ev_match(char *dir_name, char *scriptname,
        int match, len;
        FILE *fp;
 
-       sprintf(filename, "%s/bin/%s-record", dir_name, scriptname);
+       scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname);
 
        fp = fopen(filename, "r");
        if (!fp)
@@ -2799,8 +2799,8 @@ int find_scripts(char **scripts_array, char **scripts_path_array)
        }
 
        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
-               snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
-                        lang_dirent->d_name);
+               scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path,
+                         lang_dirent->d_name);
 #ifdef NO_LIBPERL
                if (strstr(lang_path, "perl"))
                        continue;
@@ -2855,8 +2855,8 @@ static char *get_script_path(const char *script_root, const char *suffix)
                return NULL;
 
        for_each_lang(scripts_path, scripts_dir, lang_dirent) {
-               snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
-                        lang_dirent->d_name);
+               scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path,
+                         lang_dirent->d_name);
                lang_dir = opendir(lang_path);
                if (!lang_dir)
                        continue;
@@ -2867,8 +2867,8 @@ static char *get_script_path(const char *script_root, const char *suffix)
                                free(__script_root);
                                closedir(lang_dir);
                                closedir(scripts_dir);
-                               snprintf(script_path, MAXPATHLEN, "%s/%s",
-                                        lang_path, script_dirent->d_name);
+                               scnprintf(script_path, MAXPATHLEN, "%s/%s",
+                                         lang_path, script_dirent->d_name);
                                return strdup(script_path);
                        }
                        free(__script_root);
index 97f64ad7fa08e236a3bde6923900e25f8b84bc1f..05dfe11c2f9e7800cbd22f2ea3b41d33d848f28f 100644 (file)
@@ -170,8 +170,8 @@ static int run_dir(const char *d, const char *perf)
        if (verbose > 0)
                vcnt++;
 
-       snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
-                d, d, perf, vcnt, v);
+       scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s",
+                 d, d, perf, vcnt, v);
 
        return system(cmd) ? TEST_FAIL : TEST_OK;
 }
index 21952e1e6e6d844f749b09cb9323b134af53f83a..0f82ee9fd3f722b9b59c45b9b5152e0696661aca 100644 (file)
@@ -16,7 +16,7 @@ static int check(union perf_mem_data_src data_src,
 
        n = perf_mem__snp_scnprintf(out, sizeof out, &mi);
        n += perf_mem__lvl_scnprintf(out + n, sizeof out - n, &mi);
-       snprintf(failure, sizeof failure, "unexpected %s", out);
+       scnprintf(failure, sizeof failure, "unexpected %s", out);
        TEST_ASSERT_VAL(failure, !strcmp(string, out));
        return 0;
 }
index 9abca267afa91dd7907dcbed79120c1247996db8..7bedf8608fdde62b931484c24c28ef8b2fd8fa9b 100644 (file)
@@ -98,7 +98,7 @@ static char *test_format_dir_get(void)
                struct test_format *format = &test_formats[i];
                FILE *file;
 
-               snprintf(name, PATH_MAX, "%s/%s", dir, format->name);
+               scnprintf(name, PATH_MAX, "%s/%s", dir, format->name);
 
                file = fopen(name, "w");
                if (!file)
index 78408f5c4bad8c0f8dae804155271eab28f50e17..decb91f9da82b54db7460beb182d9aa2598e18f4 100644 (file)
@@ -81,7 +81,7 @@ static int open_cgroup(const char *name)
        if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1))
                return -1;
 
-       snprintf(path, PATH_MAX, "%s/%s", mnt, name);
+       scnprintf(path, PATH_MAX, "%s/%s", mnt, name);
 
        fd = open(path, O_RDONLY);
        if (fd == -1)
index 4e80ca320399c577afee0bdf8c001d675a4ac755..2fb0272146d8db3c4d2397f0497d85620df28310 100644 (file)
@@ -206,8 +206,8 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
 
                for_each_event(sys_dirent, evt_dir, evt_dirent) {
 
-                       snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
-                                evt_dirent->d_name);
+                       scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
+                                 evt_dirent->d_name);
                        fd = open(evt_path, O_RDONLY);
                        if (fd < 0)
                                continue;
index 1111d5bf15ca8565766409117d3d4786636e7e29..064bdcb7bd78dcce2303bd24563bddcf42dc86d2 100644 (file)
@@ -351,7 +351,7 @@ static int pmu_aliases_parse(char *dir, struct list_head *head)
                if (pmu_alias_info_file(name))
                        continue;
 
-               snprintf(path, PATH_MAX, "%s/%s", dir, name);
+               scnprintf(path, PATH_MAX, "%s/%s", dir, name);
 
                file = fopen(path, "r");
                if (!file) {