Skip to content

Commit fb3c230

Browse files
acmelgregkh
authored andcommitted
perf tests: Avoid possible truncation with dirent->d_name + snprintf
commit 2e2bbc039fad9eabad6c4c1a473c8b2554cdd2d4 upstream. Addressing a few cases spotted by a new warning in gcc 7: tests/parse-events.c: In function 'test_pmu_events': tests/parse-events.c:1790:39: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size 90 [-Werror=format-truncation=] snprintf(name, MAX_NAME, "cpu/event=%s/u", ent->d_name); ^~ In file included from /usr/include/stdio.h:939:0, from /git/linux/tools/perf/util/map.h:9, from /git/linux/tools/perf/util/symbol.h:7, from /git/linux/tools/perf/util/evsel.h:10, from tests/parse-events.c:3: /usr/include/bits/stdio2.h:64:10: note: '__builtin___snprintf_chk' output between 13 and 268 bytes into a destination of size 100 return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ __bos (__s), __fmt, __va_arg_pack ()); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ tests/parse-events.c:1798:29: error: '%s' directive output may be truncated writing up to 255 bytes into a region of size 100 [-Werror=format-truncation=] snprintf(name, MAX_NAME, "%s:u,cpu/event=%s/u", ent->d_name, ent->d_name); Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Wang Nan <wangnan0@huawei.com> Fixes: 945aea2 ("perf tests: Move test objects into 'tests' directory") Link: http://lkml.kernel.org/n/tip-ty4q2p8zp1dp3mskvubxskm5@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 071ca0b commit fb3c230

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

tools/perf/tests/parse-events.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,27 +1727,25 @@ static int test_pmu_events(void)
17271727
}
17281728

17291729
while (!ret && (ent = readdir(dir))) {
1730-
#define MAX_NAME 100
17311730
struct evlist_test e;
1732-
char name[MAX_NAME];
1731+
char name[2 * NAME_MAX + 1 + 12 + 3];
17331732

17341733
if (!strcmp(ent->d_name, ".") ||
17351734
!strcmp(ent->d_name, ".."))
17361735
continue;
17371736

1738-
snprintf(name, MAX_NAME, "cpu/event=%s/u", ent->d_name);
1737+
snprintf(name, sizeof(name), "cpu/event=%s/u", ent->d_name);
17391738

17401739
e.name = name;
17411740
e.check = test__checkevent_pmu_events;
17421741

17431742
ret = test_event(&e);
17441743
if (ret)
17451744
break;
1746-
snprintf(name, MAX_NAME, "%s:u,cpu/event=%s/u", ent->d_name, ent->d_name);
1745+
snprintf(name, sizeof(name), "%s:u,cpu/event=%s/u", ent->d_name, ent->d_name);
17471746
e.name = name;
17481747
e.check = test__checkevent_pmu_events_mix;
17491748
ret = test_event(&e);
1750-
#undef MAX_NAME
17511749
}
17521750

17531751
closedir(dir);

0 commit comments

Comments
 (0)