linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/perf/tests: Fix compilation error with strncpy in tests/tool_pmu
@ 2024-10-13 17:37 Athira Rajeev
  2024-10-14 17:26 ` Namhyung Kim
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Athira Rajeev @ 2024-10-13 17:37 UTC (permalink / raw)
  To: acme, jolsa, adrian.hunter, irogers, namhyung
  Cc: linux-perf-users, linuxppc-dev, akanksha, maddy, atrajeev, kjain,
	disgoel, hbathini

perf fails to compile on systems with GCC version11
as below:

In file included from /usr/include/string.h:519,
                 from /home/athir/perf-tools-next/tools/include/linux/bitmap.h:5,
                 from /home/athir/perf-tools-next/tools/perf/util/pmu.h:5,
                 from /home/athir/perf-tools-next/tools/perf/util/evsel.h:14,
                 from /home/athir/perf-tools-next/tools/perf/util/evlist.h:14,
                 from tests/tool_pmu.c:3:
In function ‘strncpy’,
    inlined from ‘do_test’ at tests/tool_pmu.c:25:3:
/usr/include/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’ specified bound 128 equals destination size [-Werror=stringop-truncation]
   95 |   return __builtin___strncpy_chk (__dest, __src, __len,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   96 |                                   __glibc_objsize (__dest));
      |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~

The compile error is from strncpy refernce in do_test:
	strncpy(str, tool_pmu__event_to_str(ev), sizeof(str));

This behaviour is not observed with GCC version 8, but observed
with GCC version 11 . This is message from gcc for detecting
truncation while using strncpu. Use snprintf instead of strncpy
here to be safe.

Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
---
 tools/perf/tests/tool_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/tests/tool_pmu.c b/tools/perf/tests/tool_pmu.c
index 94d0dd8fd3cb..297cc8c55579 100644
--- a/tools/perf/tests/tool_pmu.c
+++ b/tools/perf/tests/tool_pmu.c
@@ -22,7 +22,7 @@ static int do_test(enum tool_pmu_event ev, bool with_pmu)
 	if (with_pmu)
 		snprintf(str, sizeof(str), "tool/%s/", tool_pmu__event_to_str(ev));
 	else
-		strncpy(str, tool_pmu__event_to_str(ev), sizeof(str));
+		snprintf(str, sizeof(str), "%s", tool_pmu__event_to_str(ev));
 
 	parse_events_error__init(&err);
 	ret = parse_events(evlist, str, &err);
-- 
2.43.5


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-10-16 19:05 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-13 17:37 [PATCH] tools/perf/tests: Fix compilation error with strncpy in tests/tool_pmu Athira Rajeev
2024-10-14 17:26 ` Namhyung Kim
2024-10-16 12:29   ` Athira Rajeev
2024-10-16 15:06     ` Ian Rogers
2024-10-16 18:23       ` Athira Rajeev
2024-10-16 19:04         ` Ian Rogers
2024-10-14 17:38 ` Namhyung Kim
2024-10-14 17:43 ` Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).