Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Fix snprint warnings for gcc 8

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>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
77f18153 a08f6dd4

+19 -19
+11 -11
tools/perf/builtin-script.c
··· 2674 2674 } 2675 2675 2676 2676 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 2677 - snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 2678 - lang_dirent->d_name); 2677 + scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 2678 + lang_dirent->d_name); 2679 2679 lang_dir = opendir(lang_path); 2680 2680 if (!lang_dir) 2681 2681 continue; ··· 2684 2684 script_root = get_script_root(script_dirent, REPORT_SUFFIX); 2685 2685 if (script_root) { 2686 2686 desc = script_desc__findnew(script_root); 2687 - snprintf(script_path, MAXPATHLEN, "%s/%s", 2688 - lang_path, script_dirent->d_name); 2687 + scnprintf(script_path, MAXPATHLEN, "%s/%s", 2688 + lang_path, script_dirent->d_name); 2689 2689 read_script_info(desc, script_path); 2690 2690 free(script_root); 2691 2691 } ··· 2721 2721 int match, len; 2722 2722 FILE *fp; 2723 2723 2724 - sprintf(filename, "%s/bin/%s-record", dir_name, scriptname); 2724 + scnprintf(filename, MAXPATHLEN, "%s/bin/%s-record", dir_name, scriptname); 2725 2725 2726 2726 fp = fopen(filename, "r"); 2727 2727 if (!fp) ··· 2799 2799 } 2800 2800 2801 2801 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 2802 - snprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, 2803 - lang_dirent->d_name); 2802 + scnprintf(lang_path, MAXPATHLEN, "%s/%s", scripts_path, 2803 + lang_dirent->d_name); 2804 2804 #ifdef NO_LIBPERL 2805 2805 if (strstr(lang_path, "perl")) 2806 2806 continue; ··· 2855 2855 return NULL; 2856 2856 2857 2857 for_each_lang(scripts_path, scripts_dir, lang_dirent) { 2858 - snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 2859 - lang_dirent->d_name); 2858 + scnprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, 2859 + lang_dirent->d_name); 2860 2860 lang_dir = opendir(lang_path); 2861 2861 if (!lang_dir) 2862 2862 continue; ··· 2867 2867 free(__script_root); 2868 2868 closedir(lang_dir); 2869 2869 closedir(scripts_dir); 2870 - snprintf(script_path, MAXPATHLEN, "%s/%s", 2871 - lang_path, script_dirent->d_name); 2870 + scnprintf(script_path, MAXPATHLEN, "%s/%s", 2871 + lang_path, script_dirent->d_name); 2872 2872 return strdup(script_path); 2873 2873 } 2874 2874 free(__script_root);
+2 -2
tools/perf/tests/attr.c
··· 170 170 if (verbose > 0) 171 171 vcnt++; 172 172 173 - snprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s", 174 - d, d, perf, vcnt, v); 173 + scnprintf(cmd, 3*PATH_MAX, PYTHON " %s/attr.py -d %s/attr/ -p %s %.*s", 174 + d, d, perf, vcnt, v); 175 175 176 176 return system(cmd) ? TEST_FAIL : TEST_OK; 177 177 }
+1 -1
tools/perf/tests/mem.c
··· 16 16 17 17 n = perf_mem__snp_scnprintf(out, sizeof out, &mi); 18 18 n += perf_mem__lvl_scnprintf(out + n, sizeof out - n, &mi); 19 - snprintf(failure, sizeof failure, "unexpected %s", out); 19 + scnprintf(failure, sizeof failure, "unexpected %s", out); 20 20 TEST_ASSERT_VAL(failure, !strcmp(string, out)); 21 21 return 0; 22 22 }
+1 -1
tools/perf/tests/pmu.c
··· 98 98 struct test_format *format = &test_formats[i]; 99 99 FILE *file; 100 100 101 - snprintf(name, PATH_MAX, "%s/%s", dir, format->name); 101 + scnprintf(name, PATH_MAX, "%s/%s", dir, format->name); 102 102 103 103 file = fopen(name, "w"); 104 104 if (!file)
+1 -1
tools/perf/util/cgroup.c
··· 81 81 if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1)) 82 82 return -1; 83 83 84 - snprintf(path, PATH_MAX, "%s/%s", mnt, name); 84 + scnprintf(path, PATH_MAX, "%s/%s", mnt, name); 85 85 86 86 fd = open(path, O_RDONLY); 87 87 if (fd == -1)
+2 -2
tools/perf/util/parse-events.c
··· 206 206 207 207 for_each_event(sys_dirent, evt_dir, evt_dirent) { 208 208 209 - snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 210 - evt_dirent->d_name); 209 + scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 210 + evt_dirent->d_name); 211 211 fd = open(evt_path, O_RDONLY); 212 212 if (fd < 0) 213 213 continue;
+1 -1
tools/perf/util/pmu.c
··· 351 351 if (pmu_alias_info_file(name)) 352 352 continue; 353 353 354 - snprintf(path, PATH_MAX, "%s/%s", dir, name); 354 + scnprintf(path, PATH_MAX, "%s/%s", dir, name); 355 355 356 356 file = fopen(path, "r"); 357 357 if (!file) {