perf header: Fix unchecked usage of strncpy()
tip-bot for Arnaldo Carvalho de Melo
merge botCommit-ID: 7473d42ba08f9ed4c1d7a8e72e2ad1fcd0135155 Gitweb: https://git.kernel.org/tip/7473d42ba08f9ed4c1d7a8e72e2ad1fcd0135155 Author: Arnaldo Carvalho de Melo <[email protected]> AuthorDate: Thu, 6 Dec 2018 11:02:57 -0300 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Thu, 6 Dec 2018 16:39:15 -0300
perf header: Fix unchecked usage of strncpy()
The strncpy() function may leave the destination string buffer unterminated, better use strlcpy() that we have a __weak fallback implementation for systems without it.
This fixes this warning on an Alpine Linux Edge system with gcc 8.2:
util/header.c: In function 'perf_event__synthesize_event_update_unit':
util/header.c:3586:2: error: 'strncpy' output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
strncpy(ev->data, evsel->unit, size);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
util/header.c:3579:16: note: length computed here
size_t size = strlen(evsel->unit);
^~~~~~~~~~~~~~~~~~~Cc Adrian Hunter <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Fixes a6e5281780d1 ("perf tools: Add event_update event unit type")
Signed-off-by Arnaldo Carvalho de Melo <[email protected]>
--- tools/perf/util/header.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/header.c +1 -1
@@ -3583,7 +3583,7 @@ perf_event__synthesize_event_update_unit(struct perf_tool *tool,if (ev ==NULL )return -ENOMEM ; -strncpy (ev->data, evsel->unit, size); +strlcpy (ev->data, evsel->unit, size +1 ); err =process (tool, (union perf_event *)ev,NULL ,NULL );free (ev);return err;