Skip to contentSkip to conversation

perf ftrace: Fix leak in parse_filter_event

PATCH Applied Michail Tatas 2 participants 2 messages Aug 3 – Aug 4 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf ftrace: Fix leak in parse_filter_event

strsep() advances the pointer given to it. After the loop s is either NULL (on success) or points mid buffer (early exit if malloc fails) so the original buffer is never freed properly. Fix by adding a tmp pointer for use by strsep and free the original pointer

---
 tools/perf/builtin-ftrace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +3 -2
index f7126196b092..4f881a40c311 100644
@@ -1607,14 +1607,15 @@ static int parse_filter_event(const struct option *opt, const char *str,
 {
 	struct list_head *head = opt->value;
 	struct filter_entry *entry;
-	char *s, *p;
+	char *s, *p, *tmp;
 	int ret = -ENOMEM;
 
 	s = strdup(str);
 	if (s == NULL)
 		return -ENOMEM;
 
-	while ((p = strsep(&s, ",")) != NULL) {
+	tmp = s;
+	while ((p = strsep(&tmp, ",")) != NULL) {
 		entry = malloc(sizeof(*entry) + strlen(p) + 1);
 		if (entry == NULL)
 			goto out;
Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 8 lines · Michail Tatas

strsep() advances the pointer given to it. After the loop s is either NULL (on success) or points mid buffer (early exit if malloc fails) so the original buffer is never freed properly. Fix by adding a tmp pointer for use by strsep and free the original pointer

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung