[PATCH] perf parse-events: Use asprintf() instead of strncpy() for tracepoints

From: Arnaldo Carvalho de Melo
Date: Mon Mar 02 2020 - 09:55:42 EST


Hi,

Noticed this with gcc 10 on fedora rawhide:

In file included from /usr/include/string.h:495,
from util/parse-events.h:12,
from util/parse-events.c:18:
In function âstrncpyâ,
inlined from âtracepoint_id_to_pathâ at util/parse-events.c:271:5:
/usr/include/bits/string_fortified.h:106:10: error: â__builtin_strncpyâ offset [275, 511] from the object at âsys_direntâ is out of the bounds of referenced subobject âd_nameâ with type âchar[256]â at offset 19 [-Werror=array-bounds]
106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/dirent.h:61,
from util/parse-events.c:5:
util/parse-events.c: In function âtracepoint_id_to_pathâ:
/usr/include/bits/dirent.h:33:10: note: subobject âd_nameâ declared here
33 | char d_name[256]; /* We must not include limits.h! */
| ^~~~~~
In file included from /usr/include/string.h:495,
from util/parse-events.h:12,
from util/parse-events.c:18:
In function âstrncpyâ,
inlined from âtracepoint_id_to_pathâ at util/parse-events.c:273:5:
/usr/include/bits/string_fortified.h:106:10: error: â__builtin_strncpyâ offset [275, 511] from the object at âevt_direntâ is out of the bounds of referenced subobject âd_nameâ with type âchar[256]â at offset 19 [-Werror=array-bounds]
106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/dirent.h:61,
from util/parse-events.c:5:
util/parse-events.c: In function âtracepoint_id_to_pathâ:
/usr/include/bits/dirent.h:33:10: note: subobject âd_nameâ declared here
33 | char d_name[256]; /* We must not include limits.h! */
| ^~~~~~
CC /tmp/build/perf/util/call-path.o

So I replaced it with asprintf to make the code shorter, use a bit less
memory and deal with the above problem, ok?

- Arnaldo

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index c01ba6f8fdad..a14995835d85 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -257,21 +257,15 @@ struct tracepoint_path *tracepoint_id_to_path(u64 config)
path = zalloc(sizeof(*path));
if (!path)
return NULL;
- path->system = malloc(MAX_EVENT_LENGTH);
- if (!path->system) {
+ if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
free(path);
return NULL;
}
- path->name = malloc(MAX_EVENT_LENGTH);
- if (!path->name) {
+ if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
zfree(&path->system);
free(path);
return NULL;
}
- strncpy(path->system, sys_dirent->d_name,
- MAX_EVENT_LENGTH);
- strncpy(path->name, evt_dirent->d_name,
- MAX_EVENT_LENGTH);
return path;
}
}