[PATCH] perf list: Fix memleak in print_sdt_events()

From: Wei Li
Date: Sat Jun 20 2020 - 02:47:17 EST


Valgrind check info:
==30629== Command: ./perf list sdt
==30629==
==30629==
==30629== HEAP SUMMARY:
==30629== in use at exit: 12,226 bytes in 195 blocks
==30629== total heap usage: 3,239 allocs, 3,044 frees, 3,546,759 bytes allocated
==30629==
==30629== 8,028 bytes in 115 blocks are definitely lost in loss record 54 of 54
==30629== at 0x4885E44: realloc (vg_replace_malloc.c:785)
==30629== by 0x56468C3: __vasprintf_chk (in /usr/lib64/libc-2.28.so)
==30629== by 0x5646723: __asprintf_chk (in /usr/lib64/libc-2.28.so)
==30629== by 0x4E2C3F: asprintf (stdio2.h:181)
==30629== by 0x4E2C3F: print_sdt_events (parse-events.c:2611)
==30629== by 0x446587: cmd_list (builtin-list.c:87)
==30629== by 0x4B8947: run_builtin (perf.c:312)
==30629== by 0x4B8C13: handle_internal_command (perf.c:364)
==30629== by 0x434717: run_argv (perf.c:408)
==30629== by 0x434717: main (perf.c:538)
==30629==
==30629== LEAK SUMMARY:
==30629== definitely lost: 8,028 bytes in 115 blocks
==30629== indirectly lost: 0 bytes in 0 blocks
==30629== possibly lost: 0 bytes in 0 blocks
==30629== still reachable: 4,198 bytes in 80 blocks
==30629== suppressed: 0 bytes in 0 blocks

Signed-off-by: Wei Li <liwei391@xxxxxxxxxx>
---
tools/perf/util/parse-events.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 3decbb203846..b2acacd5646e 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -2579,13 +2579,12 @@ void print_sdt_events(const char *subsys_glob, const char *event_glob,
struct probe_cache *pcache;
struct probe_cache_entry *ent;
struct strlist *bidlist, *sdtlist;
- struct strlist_config cfg = {.dont_dupstr = true};
struct str_node *nd, *nd2;
char *buf, *path, *ptr = NULL;
bool show_detail = false;
int ret;

- sdtlist = strlist__new(NULL, &cfg);
+ sdtlist = strlist__new(NULL, NULL);
if (!sdtlist) {
pr_debug("Failed to allocate new strlist for SDT\n");
return;
@@ -2610,8 +2609,10 @@ void print_sdt_events(const char *subsys_glob, const char *event_glob,
continue;
ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
ent->pev.event, nd->s);
- if (ret > 0)
+ if (ret > 0) {
strlist__add(sdtlist, buf);
+ free(buf);
+ }
}
probe_cache__delete(pcache);
}
--
2.19.1