Skip to contentSkip to conversation

perf header: Fix unchecked usage of strncpy()

tip:perf/core tip-bot for Arnaldo Carvalho de Melo 1 participant 1 message Dec 14, 2018 perf events

lore
Share & tools
lore
#0 lore raw
Commit-ID:  8b3ff080ebb16abcb4766e916dc020add0f4df39
Gitweb:     https://git.kernel.org/tip/8b3ff080ebb16abcb4766e916dc020add0f4df39
Author:     Arnaldo Carvalho de Melo <[email protected]>
AuthorDate: Thu, 6 Dec 2018 11:09:46 -0300
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Thu, 6 Dec 2018 16:41:06 -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_name':
util/header.c:3625: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->name, len);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  util/header.c:3618:15: note: length computed here
    size_t len = strlen(evsel->name);
                 ^~~~~~~~~~~~~~~~~~~
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")
---
 tools/perf/util/header.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/header.c +1 -1
index c87cfe6b7c96..1171d8400bf4 100644
@@ -3622,7 +3622,7 @@ perf_event__synthesize_event_update_name(struct perf_tool *tool,
 	if (ev == NULL)
 		return -ENOMEM;
 
-	strncpy(ev->data, evsel->name, len);
+	strlcpy(ev->data, evsel->name, len + 1);
 	err = process(tool, (union perf_event*) ev, NULL, NULL);
 	free(ev);
 	return err;