Skip to contentSkip to conversation

perf lock: Avoid memory leaks from strdup

PATCH Applied Ian Rogers 3 participants 3 messages May 9, 2024 – May 10, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf lock: Avoid memory leaks from strdup

Leak sanitizer complains about the strdup-ed arguments not being freed and given cmd_record doesn't modify the given strings, remove the strdups.

Original discussion in this patch:
https://lore.kernel.org/lkml/[email protected]/

---
 tools/perf/builtin-lock.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
tools/perf/builtin-lock.c +4 -14
index 230461280e45..7007d26fe654 100644
@@ -2275,23 +2275,13 @@ static int __cmd_record(int argc, const char **argv)
 		return -ENOMEM;
 
 	for (i = 0; i < ARRAY_SIZE(record_args); i++)
-		rec_argv[i] = strdup(record_args[i]);
+		rec_argv[i] = record_args[i];
 
 	for (j = 0; j < nr_tracepoints; j++) {
-		const char *ev_name;
-
-		if (has_lock_stat)
-			ev_name = strdup(lock_tracepoints[j].name);
-		else
-			ev_name = strdup(contention_tracepoints[j].name);
-
-		if (!ev_name) {
-			free(rec_argv);
-			return -ENOMEM;
-		}
-
 		rec_argv[i++] = "-e";
-		rec_argv[i++] = ev_name;
+		rec_argv[i++] = has_lock_stat
+			? lock_tracepoints[j].name
+			: contention_tracepoints[j].name;
 	}
 
 	for (j = 0; j < nr_callgraph_args; j++, i++)
Signature · 1 line
2.45.0.rc1.225.g2a3ae87e7f-goog
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Ian Rogers

Leak sanitizer complains about the strdup-ed arguments not being freed and given cmd_record doesn't modify the given strings, remove the strdups.

Original discussion in this patch:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Namhyung

Quoted 39 lines
---
 tools/perf/builtin-lock.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
tools/perf/builtin-lock.c +4 -15
index 230461280e45..7007d26fe654 100644
@@ -2275,23 +2275,13 @@ static int __cmd_record(int argc, const char **argv)
                return -ENOMEM;
 
        for (i = 0; i < ARRAY_SIZE(record_args); i++)
-               rec_argv[i] = strdup(record_args[i]);
+               rec_argv[i] = record_args[i];
 
        for (j = 0; j < nr_tracepoints; j++) {
-               const char *ev_name;
-
-               if (has_lock_stat)
-                       ev_name = strdup(lock_tracepoints[j].name);
-               else
-                       ev_name = strdup(contention_tracepoints[j].name);
-
-               if (!ev_name) {
-                       free(rec_argv);
-                       return -ENOMEM;
-               }
-
                rec_argv[i++] = "-e";
-               rec_argv[i++] = ev_name;
+               rec_argv[i++] = has_lock_stat
+                       ? lock_tracepoints[j].name
+                       : contention_tracepoints[j].name;
        }
 
        for (j = 0; j < nr_callgraph_args; j++, i++)
--

2.45.0.rc1.225.g2a3ae87e7f-goog

maintainer Applied
#2 lore raw
Quoted 6 lines · Namhyung Kim
Quoted 5 lines · Ian Rogers

Leak sanitizer complains about the strdup-ed arguments not being freed and given cmd_record doesn't modify the given strings, remove the strdups.

Quoted 7 lines
Quoted 5 lines

Original discussion in this patch:
https://lore.kernel.org/lkml/[email protected]/

Thanks, applied to perf-tools-next,

- Arnaldo